How To Add, Modify and Delete JavaScript Object Literal Properties

--

JavaScript object is a collection of properties, and a property is an association between a name (or key) and a value. And we as developers use it excessively. In the initial days of my programming career, I found it difficult to work with the object manipulation. So today I would like to list out the ways to add, update and delete the properties from an object.

Add property to an Object:

One can add the property to an object by simply giving it a value. Like below example, we are adding the property of husband and giving the value directly. We can use bracket while assigning the value too.

One can use ES7 Syntax and functional approach and add the property which would yield the same result.

Delete property from an Object:

One can delete the property from the object using keyword delete . The delete keyword deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again.

Update the value of the existing property

One can update the value of the property just by reassigning the value to the same key.

Add the properties to the array of Object

Consider we have an array of objects and we want to add the property to every object in the array. We can achieve this using many array methods(also using for loop) . But here I have used the array method .forEach to iterate through the array and add the property to the object.

Delete the properties from the array of Object

Here, delete is done as similar to the addition. The iteration is done using array method .forEach and then the deletion is done using keyword delete.

Update every values of the existing property in the array of Objects

Here the array method .every is used to iterate through the elements of the array. The property 'responsibility' is reassigned (‘heart of the show to ‘making people laugh’) to different value.

I have listed few of the ways I know how to add, update and delete the properties. Comment below if you know any other ways.

Thank you ..

--

--