javascript push object code example
Example 1: push array javascript
let array = ["A", "B"];
let variable = "what you want to add";
array.push(variable);
console.log(array);
Example 2: js push array
array.push(element_to_push);
Example 3: js push in object
let obj = {};
let objToAdd1 = { prop1: "1", prop2: "2" };
let objToAdd2 = { prop3: "3", prop4: "4" };
obj = { ...obj, ...objToAdd1 };
obj = { ...obj, ...objToAdd2 };
console.log(obj);
Example 4: how to append object in array javascript
var select =[2,5,8];
var filerdata=[];
for (var i = 0; i < select.length; i++) {
filerdata.push(this.state.data.find((record) => record.id == select[i]));
}
Example 5: push in object javascript
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;
Example 6: push in object javascript
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;