DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

How to update the attribute value of an object using the map function in JavaScript?

To update the attribute value of an object using the map function in JavaScript, we return the updated object.

For instance, we write

const editSchoolName = (schools, oldName, name) =>
  schools.map((item) => {
    if (item.name === oldName) {
      return { ...item, name };
    } else {
      return item;
    }
  });
Enter fullscreen mode Exit fullscreen mode

to define the editSchooName function that returns a new version of the schools array that has entries mapped from schools with the objects updated if they have name property equal to oldName.

Top comments (0)