how to add key in javascript code example

Example 1: js add key to object

// given
const obj = {
    key1: value1,
    key2: value2
};

// add pair
obj["key3"] = value3;
obj.key4 = value4;

Example 2: javascript add to object

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

// Array of Objects in form {element: {id: 10, quantity: 10} }
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});