array push object javascript code example

Example 1: javascript array push

var SomeRandomArray = [];
SomeRandomArray.push("Hello, world!");

Example 2: how to add objects in array

var a=[], b={};
a.push(b);    
// a[0] === b;

Example 3: 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 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]));
}
//I have a data which is object,
//find method return me the filter data which are objects
//now with the push method I can make array of objects

Example 5: how to add object to list in javascript

var a=[]
var b={};
a.push(b);

Example 6: push object into array javascript

var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);