add item in object javascript code example
Example 1: 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 2: javascript add to object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Example 3: adding element to javascript object
let person = {
name : 'John Doe',
age : 35
}
person.occupation = 'Web Designer'
person['occupation'] = 'Web Designer';
object[yourKey] = yourValue;
object.yourKey = yourValue;
Example 4: js add data in object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);