update value of object javascript code example

Example 1: how to add property to object in javascript

var data = {
    'PropertyA': 1,
    'PropertyB': 2,
    'PropertyC': 3
};

data["PropertyD"] = 4;

// dialog box with 4 in it
alert(data.PropertyD);
alert(data["PropertyD"]);

Example 2: js change value in object

var object = { boo:true, baa:5 };
console.log(object);
function change() {
  object.boo = false;
  object.baa++;
};
change();
console.log(object);
//Hope this helps!

Example 3: how to update object in javascript

myObject['first name'] = 'John'; // property name with a space

Example 4: update object within object by id js

//This will replace myId of the item id with the updated object

return myArray.map((item) => {
	return item.id === myId
		? UPDATED_OBJECT_HERE
		: item;
});