How to modify an object’s property in an array of objects in JavaScript

we will try to understand how we may modify an object’s property in an array of objects in JavaScript using an example that contains a valid as well as justified approach.

Let us first try to understand how we may create an array with multiple objects with the help of the below-enlightened syntax.

Syntax: The following syntax will help us to understand how we may easily create an array containing multiple objects:

let array_of_objects = [
    {
        property_name: value,
        ...
    },
    {
        property_name: value,
        ...
    },
    ...
]

Now that we have seen the above syntax for creating an array with multiple objects, let us have a quick look at the following example which will help us to understand the above syntax more clearly.

Example: In this example, we will try to create an array with multiple objects and then by using Array.map() method we will output all the objects which are present inside our array itself.

let employees_data = [
		{
			employee_id: 1,
			employee_name: "Aman",
		},
		{
			employee_id: 2,
			employee_name: "Bhargava",
		},
		{
			employee_id: 3,
			employee_name: "Chaitanya",
		},
	];
	employees_data.map((element) => {
		console.log(element);
	});
Output:

[
    { employee_id: 1, employee_name: 'Aman' },
    { employee_id: 2, employee_name: 'Anthony' },
    { employee_id: 3, employee_name: 'Chaitanya' }
]

Leave a comment

Your email address will not be published. Required fields are marked *