javascript push item to objcet code example

Example 1: 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 2: js add data in object

var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);