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 appends values to an 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 an array into another array if the 1st item in the array to be pushed matches a string
Since the other comment is useless, I will help out. Your code is close. Firstly, you need a third arg in your forEach function to reference the array you are looping thru: just add a third arg called groups because you already called it that within. Second, you need to be cautious of the case of the letters. Third, do not push the array. You need to use concat to join them; otherwise you push the whole array into the other, not just the values. Go back and fix these things. Still will not be perfect but these are good things to note/fix. Edit. Just a few btws: your code also has an issue that if the first subarray contains a keyword, an index out of bound error will be thrown. Also, the array that is added to the array before it will still exist. Your array length will not change. Not sure if this is your intention or not, just pointing it out. More on reddit.com
Help: Pushing new objects into an array in JavaScript
Currently in the "JavaScript Loops, Arrays and Objects". While working on the objective of simply creating a list of objects in an array and then printing them, I decided to challenge myself more. To this end I tried to create a prompt set up that will do the following: Ask a series of 4 questions. Take the answers and create a new student object. Take that new student object and push ... More on teamtreehouse.com
Cannot use push on array
It seems like the program consider the “array” is not an Array Object, so I cannot use push there, just wondering why · I cleaned up your code. You need to use triple backticks to post code to the forum. See this post for details · You’re trying to push into a specific index (array[i]). ... More on forum.freecodecamp.org
Trying to push to an array in an async function, array remains empty
Questions and posts about frontend ... to JavaScript on the backend. ... I have a function on a website backend I am making which does some uploads and saves some things to a database, but I am having trouble pushing values returned from an inner function to an array within the ... More on reddit.com
Videos
02:01
JavaScript tips — Add multiple values to an array using Array.push ...
What Is The Array Push Method In Javascript? - YouTube
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 ...
01:27
push Array Method | JavaScript Tutorial - YouTube
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;
Reddit
reddit.com › r/learnjavascript › how to push an array into another array if the 1st item in the array to be pushed matches a string
r/learnjavascript on Reddit: How to push an array into another array if the 1st item in the array to be pushed matches a string
November 10, 2023 -
Let's say I have the array of arrays:
groups = [ 0: ['FOLDER', 'file', 'last file'] 1: ['FOLDER', 'SUB folder', 'file', 'file', 'last file'] 2: ['SUB folder', 'file', 'last file'] 3: ['file', 'file', 'last file'] 4: ['FOLDER', 'last file', 'SUB folder', 'file', 'last file'] 5: ['last file'] ]
I want to push every sub-array in the "groups" array into the previous sub-array, if the sub-array's 1st item contains the 'last' or 'sub' strings. How do I do this?
This is what I'm trying but it doesn't work:
groups.forEach(function(group, i) {
if (group[0].includes('sub') || group[0].includes('last')) {
groups[i - 1].push(group);
}
}); So, for each sub-array in "groups" array, if the sub-array's first item contains string1 or string2, push it into the previous array in the loop.
Edit: thanks for the replies everyone, though I ended up going for a single one, I learned a bit about iteration from all of you.
Top answer 1 of 5
1
Since the other comment is useless, I will help out. Your code is close. Firstly, you need a third arg in your forEach function to reference the array you are looping thru: just add a third arg called groups because you already called it that within. Second, you need to be cautious of the case of the letters. Third, do not push the array. You need to use concat to join them; otherwise you push the whole array into the other, not just the values. Go back and fix these things. Still will not be perfect but these are good things to note/fix. Edit. Just a few btws: your code also has an issue that if the first subarray contains a keyword, an index out of bound error will be thrown. Also, the array that is added to the array before it will still exist. Your array length will not change. Not sure if this is your intention or not, just pointing it out.
2 of 5
1
Instead of using forEach() and trying to push into the current array during the iteration you create a new array with reduce(). In the callback if the index is 0 or the first element of the current array does not containt the substrings "last" or "sub", case-insensitive, there is no previous element to push the contents of the current array into, so we spread the new array and set the current array into a new array, else we push into the previous array and return the new array. let grouped = groups.reduce((a, b, index) => !index || !/last|sub/i.test(b[0]) ? [...a, b] : (a[a.length -1].push(...b), a) , []); // groups = grouped;
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-push-method
JavaScript Array push() Method - GeeksforGeeks
The array push() function adds one or more values to the end of the array and returns the new length. This method changes the length of the array. An array can be inserted into the object with push() function.
Published April 15, 2025
freeCodeCamp
freecodecamp.org › news › how-to-insert-an-element-into-an-array-in-javascript
Push into an Array in JavaScript – How to Insert an Element into an Array in JS
November 7, 2024 - This article will show you how to insert an element into an array using JavaScript. In case you're in a hurry, here are the methods we'll be discussing in depth in this article: // Add to the start of an array Array.unshift(element); // Add to the end of an array Array.push(element); // Add to a specified location Array.splice(start_position, 0, new_element...); // Add with concat method without mutating original array let newArray = [].concat(Array, element);
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › from
Array.from() - JavaScript | MDN
A function to call on every element of the array. If provided, every value to be added to the array is first passed through this function, and mapFn's return value is added to the array instead.
Stack Abuse
stackabuse.com › bytes › push-an-object-to-an-array-in-javascript
Push an Object to an Array in JavaScript
July 23, 2022 - To add multiple objects to an array, you can pass multiple objects as arguments to the push() method, which will add all of the items to the end of the array.
Scaler
scaler.com › topics › javascript-array-push
JavaScript Array push() Method - Scaler Topics
June 7, 2023 - In the example above, the push() ... array is displayed using console.log(). To append new element to the end of a JavaScript array, you can use the push() method....
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › unshift
Array.prototype.unshift() - JavaScript | MDN
Array.prototype.push() has similar behavior to unshift(), but applied to the end of an array. Please note that, if multiple elements are passed as parameters, they're inserted in chunk at the beginning of the object, in the exact same order they were passed as parameters.