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 Overflowpush() 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;
Objects does not support push property, but you can save it as well using the index as key,
var tempData = {};
for ( var index in data ) {
if ( data[index].Status == "Valid" ) {
tempData[index] = data;
}
}
data = tempData;
I think this is easier if remove the object if its status is invalid, by doing.
for(var index in data){
if(data[index].Status == "Invalid"){
delete data[index];
}
}
And finally you don't need to create a var temp โ
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 :)
Help: Pushing new objects into an array in JavaScript
Pushing Objects Into Array
How to push data to nested object in JavaScript
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
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.