How to push an object in an Array
const animals = ['pigs', 'goats', 'sheep'];
animals.push({animal: 'cows'});
console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]
Answer from Soham on Stack OverflowMDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Array โบ push
Array.prototype.push() - JavaScript | MDN
The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.
W3Schools
w3schools.com โบ jsref โบ jsref_push.asp
JavaScript Array push() Method
The push() method adds new items to the end of an array.
How to push object in array Javascript? - Stack Overflow
I see that you're starting learning JavaScript. I recommend you to read the docs about Arrays and Objects. More on stackoverflow.com
javascript - How to use Array.push and return the pushed item? - Stack Overflow
The fact alone that this answer requires an explanation to validate that it causes the same side-effect (due to how the array exotic object modifies its length property when a value is assigned to a slot with an index greater than or equal to its length), I'm pretty sure it would fall into ... More on stackoverflow.com
Help: Pushing new objects into an array in JavaScript
Dillon Wyatt is having issues with: Hello, I am working through the JavaScript course now. Currently in the "JavaScript Loops, Arrays and Objects". While working on the ob... More on teamtreehouse.com
How to push Array in to nested Object? I cant use .push() since im trying to add a new key value pair? Ive tried looking at stack overflow and other places but no luck. if anyone can point me in the right direction would be super helpful please :)
Push is an array method but your trying to add data to an object so that isnt going to work. To add data to an obect you need to either: obj.key = ['array'] obj['key'] = ['array'] If an object key is already an array you can: obj.key.push('something) edit: realised it is an array, so the above follows but you want: array[index].key = ['item'] array[index][key] = ['item'], or if already an array array[index][key].push('item) More on reddit.com
Videos
01:37
JS Array Methods Explained #10 - PUSH Method - YouTube
JavaScript Array Methods in Minutes: PUSH( ) โ 3 EXAMPLES! - YouTube
push( ) โ JavaScript Array Methods in 3 Mins or Less (3 ...
02:01
JavaScript tips โ Add multiple values to an array using Array.push ...
What Is The Array Push Method In Javascript? - YouTube
01:27
push Array Method | JavaScript Tutorial - YouTube
Slack
docs.slack.dev โบ methods โบ chat.postmessage
chat.postMessage method | Slack Developer Docs
When POSTing with application/x-www-form-urlencoded data, the optional attachments argument should contain a JSON-encoded array of attachments.
Top answer 1 of 5
2
How to push an object in an Array
const animals = ['pigs', 'goats', 'sheep'];
animals.push({animal: 'cows'});
console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]
2 of 5
1
I think like this:
var CatTitle = ['Travel', 'Daily Needs','Food & Beverages','Lifestyle','Gadget & Entertainment','Others'];
var myObj = {Coupon exp : 'xxx', couponcode : 'xxx'};
var newObject = {};
var newArray = [];
var i;
for(i=0; i < CatTitle.length; i++) {
var dump = {
CatTitle[i]: myObj
}
newArray.push(dump);
}
newObject = newArray;
Top answer 1 of 3
10
You could also do it using a logical and (&&).
return someArr.push(newItem) && newItem;
Since the array length will be positive after the push, the right-most expression will be evaluated and its value returned.
This syntax is less clear than @adiga's answer using the comma operator though.
However, I personally prefer to return the new item on a new line to make it more readable:
someArr.push(newItem);
return newItem;
2 of 3
7
You could use the comma operator:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
Like this:
return (someArr.push(newItem), newItem)
This is equivalent to:
someArr.push(newItem);
return newItem
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ javascript-arrays
JavaScript Arrays - GeeksforGeeks
The push() method add the element to the end of the array.
Published ย October 3, 2025
React
react.dev โบ reference โบ react โบ StrictMode
<StrictMode> โ React
This StoryTray component takes an array of stories and adds one last โCreate Storyโ item at the end: ... export default function StoryTray({ stories }) { const items = stories; items.push({ id: 'create', label: 'Create Story' }); return ( <ul> {items.map(story => ( <li key={story.id}> {story.label} </li> ))} </ul> ); }
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ javascript-array-push-method
JavaScript Array push() Method - GeeksforGeeks
The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array.
Published ย April 15, 2025
W3Schools Blog
w3schools.blog โบ home โบ javascript array push()
JavaScript Array push() - W3schools
April 20, 2019 - The JavaScript array push() method is used to add one or more elements to the end of an array.
JavaScript Tutorial
javascripttutorial.net โบ home โบ javascript array methods โบ array.prototype.push()
JavaScript Array Push
November 6, 2024 - The JavaScript Array push() method adds one or more elements to the end of an array and returns the array's length.