create key value pair in javascript code example
Example 1: how to add field to object in js
var obj = {
key1: "a",
key2: "b"
};
obj.key3 = "c";
obj["key3"] = "c";
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: key value pair array in javascript
var myArray = {id1: 100, id2: 200, "tag with spaces": 300};
myArray.id3 = 400;
myArray["id4"] = 500;
Example 4: javascript create object from key value pairs
let items = 'key1:value1;key2:value2;key3:value3';
let items_array = items.split(';');
let final_items = items_array.map(item => item.split(':'));
let final_result = Object.fromEntries(final_items);