Skip to content
Advertisement

Update the attribute value of an object using the map function in ES6

I am trying to code this in ES6. Below is what I am trying to achieve. Let’s say I have an array of objects called schools.

JavaScript

Now, I want to write a function called editSchoolName which will take 3 parameters, schools (which is the array I have defined above), oldName and name.

I will pass the name of the school in the parameter oldName and that name should be updated with the value in the parameter name.

I don’t want to change the state of the variable schools so I am using a map function which will return a new array with the changes.

The editSchoolName function will be called like this –

JavaScript

Here, the name YorkTown should be replaced with the name New Gen. So the expected value of the array updatedSchools should be –

JavaScript

This is how my editSchoolName function looks like –

JavaScript

Need help in making the change in the editSchoolName function to achieve the above mentioned desired result.

Advertisement

Answer

try this, ES6 Object.assign() to create copy of array element and update new object.

JavaScript

Using destructuring

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement