push to object array javascript code example

Example 1: javascript add items to array

//adding items to array
objects = [];
objects.push("you can add a string,number,boolean,etc");
//if you more stuff in a array
//before i made a error doing value = : value
objects.push({variable1 : value, variable2 : value);
//we do the {} so we tell it it will have more stuff and the variable : value

Example 2: js push in object

let obj = {};
let objToAdd1 = { prop1: "1", prop2: "2" };
let objToAdd2 = { prop3: "3", prop4: "4" };

// obj variabile could be empty or not, it's the same
obj = { ...obj, ...objToAdd1 };
obj = { ...obj, ...objToAdd2 };

// Note that i used the spread operator... This syntax is available
// only for the most recent js ES (from ES6 on, if i'm not wrong) :)

console.log(obj);

Example 3: how to append objects to javascript lists ?

var studentList = ['Jason', 'Samantha', 'Alice', 'Joseph'];

//Add a new student to the end of the student list
studentList.push('Jacob');
//list is updated to ['Jason', 'Samantha', 'Alice', 'Joseph', 'Jacob'];

Example 4: how to push in object in 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 5: javascript array push object

var myArray = [];
myArray.push("Example");
myArray.push("text");