push() is for arrays, not objects, so use the right data structure.

var data = [];
// ...
data[0] = { "ID": "1", "Status": "Valid" };
data[1] = { "ID": "2", "Status": "Invalid" };
// ...
var tempData = [];
for ( var index=0; index<data.length; index++ ) {
    if ( data[index].Status == "Valid" ) {
        tempData.push( data );
    }
}
data = tempData;
Answer from Matt Ball on Stack Overflow
Discussions

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
๐ŸŒ r/learnjavascript
9
22
March 8, 2022
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
๐ŸŒ teamtreehouse.com
1
April 22, 2018
Pushing Objects Into Array
storage.push({ [userInputAmount.value]: userInputProduct.value }); or alternatively const obj = {}; obj[userInputAmount.value] = userInputProduct.value; storage.push(obj); Out of curiosity, it seems weird to have an array of objects that each have different keys. More typically, you'd structure it in one of the following ways: // single object { 'Red Bull': '37', 'Pizza': '35', } // array of arrays [ ['Red Bull', '37'], ['Pizza', '35'], ] // array of uniform objects [ { name: 'Red Bull', value: '37' }, { name: 'Pizza', value: '35' }, ] because that way it's easier to iterate over the items. Is there a reason you opted for [ { 'Red Bull': '37' }, { 'Pizza': '35' }, ] ? More on reddit.com
๐ŸŒ r/learnjavascript
8
2
November 8, 2021
How to push data to nested object in JavaScript
I think you've got it the wrong way around. The thing that you want to keep adding items to should be an array and each item should apparently be an object. var orders = { theOrderNum: [ { itemName: "Coffee", itemQty: 1 }, { itemName: "Chips", itemQty: 2 } ], theOtherOrderNum: [ { itemName: "Apples", itemQty: 2 }, { itemName: "Oranges", itemQty: 1 } ] }; orders.theOrderNum.push({ itemName: "Bananas", itemQty: 3 }); orders.theOtherOrderNum.push({ itemName: "Pears", itemQty: 2 }); You could also do this var orders = { theOrderNum: { Coffee: { itemQty: 1 }, Chips: { itemQty: 2 } }, theOtherOrderNum: { Apples: { itemQty: 2 }, Oranges: { itemQty: 1 } } }; orders.theOrderNum.Bananas = 3; or even this var orders = { theOrderNum: { Coffee: 1, Chips: 2 }, theOtherOrderNum: { Apples: 2, Oranges: 1 } }; orders.theOrderNum.Bananas = 3; In those two options, each kind of item can only appear once in each order (with a certain quantity), which may or may not be what you want. More on reddit.com
๐ŸŒ r/learnjavascript
7
3
April 1, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-push-an-object-into-another-object-in-javascript
How to Push an Object into Another Object in JavaScript ? - GeeksforGeeks
July 23, 2025 - In JavaScript, the concept of pushing an object into another object involves embedding one object within another as a property.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Object โ€บ assign
Object.assign() - JavaScript | MDN
The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
Find elsewhere
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Object
Object - JavaScript | MDN
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
Top answer
1 of 1
1
Objects behave a bit differently from most other datatypes in JavaScript. When you assign an object to a variable or add it to an array or anything like that. JavaScript does not make a copy of the object, it makes the new variable (or array item) contain the exact same object you assigned. To illustrate this take a look at this code: ```JavaScript var example = { message: "Hello" }; var example2 = example; example.message = "world"; console.log(example2); ``` We create an object example then we create a new variable example2 and assign example to it. Then we change the message property of example and then we print out example2. Now what do you think will be printed out? If Objects worked like Strings and Numbers then you would expect { message: 'Hello' } to be printed out. Since that is what example contained when it was assigned to example2. But in reality the above code prints out { message: 'world' }. This is because these two variables don't hold independent copies of the object, they hold the exact same object. Which means that even though there are two variables at play there is actually only one object created in the above code. You just have two variables pointing at it. With that knowledge if you take a look at your code the behavior you experience should make a bit more sense. You create one object at the start of your code, then you assign it the details of one student. Then you add a pointer to that object to the array. Then you change that same object with details of a new student. Since you in reality only have one object you end up changing the values of that one object each time the loop runs. And adding that object to the array multiple times just creates multiple pointers to it. The solution to this issue is quite simple. Just create the object within the loop. Like this: ```JavaScript alert ("Hello"); var students = []; var tempNewStudentStorage; var finished; var questions = [ "What is the student's name?", "What Track(s) are they on?", "What achivement(s) have they gotten?", "What are their points?", ]; while (true) { var newStudent = { Name: "x", Track: "y", Achivement: 0, Points: 0, }; for (var i = 0; i < 4; i +=1) { tempNewStudentStorage = prompt(questions[i]); if (i === 0){ newStudent.Name = tempNewStudentStorage; } else if (i === 1){ newStudent.Track = tempNewStudentStorage; } else if (i === 2){ newStudent.Achievemnt = tempNewStudentStorage; } else if (i === 3){ newStudent.Points = tempNewStudentStorage; }; }; students.push(newStudent); finished = prompt("Are you done adding students? Y/N").toLowerCase(); while (finished.length > 1) { finished = prompt("Are you done adding students? Y/N").toLowerCase() }; if (finished === "y") { alert("Thank you for adding a new student!") break } else if (finished !== "y"){ continue; }; }; console.log(students); ``` That way you actually create a new object each time the loop runs, instead of reusing the object created at the start of the script. It's worth noting that Arrays and Functions also behave in the same way as I described above.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ push-an-object-to-an-array-in-javascript
Push an Object to an Array in JavaScript
July 23, 2022 - let array = []; let obj = { name: 'Billy', age: 30, role: 'admin' }; array.push(obj); console.log(array); // [{name: 'Billy', age: 30, role: 'admin'}] As you can see, we simply pass the obj object to the push() method and it will add it to the end of the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-add-an-object-to-an-array-in-javascript
JavaScript- Add an Object to JS Array - GeeksforGeeks
The push() method adds objects to the end, unshift() adds to the beginning, and concat() combines arrays. The spread operator offers a modern way to append objects, while splice() allows insertion at specific indices.
Published: July 12, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ pushing objects into array
r/learnjavascript on Reddit: Pushing Objects Into Array
November 8, 2021 -

Ive been googling for a fair bit but i simply dont understand how i should do this.

Ive got two input fields, in one field the user inputs the product name, and in the second input field the user inputs how much of this product to be added to the storage. For example "Red Bull" and "37". I then want to take these inputs and push them into an array that ive created. However i want to make them a singular object within the array so that it doesent look like this: ["Red Bull", "37", "Pizza", "35"] but rather [{"Red Bull", 37},{"Pizza", 35}] So that essentially every time the button is clicked that runs the function, a new object within the array is created. My code so far:

Any help would be greatly appriciated <3

The "addProduct" function is the one im struggling with as it pushes the items directly into the array.

Heres the working one
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-push-an-object-into-an-array-using-for-loop-in-javascript
How to Push an Object into an Array using For Loop in JavaScript ? - GeeksforGeeks
August 5, 2025 - JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ how to push data to nested object in javascript
r/learnjavascript on Reddit: How to push data to nested object in JavaScript
April 1, 2019 -

I would like to make a nested object and push data. It would look something like this:

var orders = {

theOrderNum = {

[itemName: "Coffee", itemQty: 1],

[itemName: "Chips", itemQty: 2],

//keep adding arrays

}

}

so how can I keep adding to the orderNum object? Also, I would also want to add another orderNum with different items inside.

๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
How do I push an array inside an object array - Curriculum Help - The freeCodeCamp Forum
March 18, 2022 - this is driving me nuts. how can I get the numbers in the object inside the date array. CODE LINK : โ€™ https://replit.com/@kukovisuals/data-manipulation โ€™ const data = [ {"draw_date":"2022-03-16T00:00:00.000","winning_numbers":"34 42 48 51 56 59","bonus":"6"} ] let groupNumber = {} const keys = Array.from({length: 6}, (_,y) => (y )) keys.map(key => groupNumber[key] = []) data.map( d => { // we get the numbers and days let nums = d['winning_numbers'] let days = d['draw_date'] getNumbe...
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-push-object-to-array
Push an object to an array in JavaScript
June 9, 2023 - const users = [] const obj = { name: 'John Doe', age: 23 } users.push(obj) console.log(users) // [ { name: 'John Doe', age: 23 } ] You can also add multiple objects to an array by passing them as separate arguments to the Array.push() method, as demonstrated below:
๐ŸŒ
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 - The push() method is similar to the unshift() method as it adds an element to the end of an array rather than the beginning.