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

爷,独闯天下 提交于 2019-12-07 04:58:33

问题


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.

let schools = [
    {name: 'YorkTown', country: 'Spain'},
    {name: 'Stanford', country: 'USA'},
    {name: 'Gymnasium Achern', country: 'Germany'}
];

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 -

var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");

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

let updatedSchools = [
    {name: 'New Gen', country: 'Spain'},
    {name: 'Stanford', country: 'USA'},
    {name: 'Gymnasium Achern', country: 'Germany'}
];

This is how my editSchoolName function looks like -

const editSchoolName = (schools, oldName, name) =>
    schools.map(item => {
        if (item.name === oldName) {
          /* This is the part where I need the logic */
        } else {
          return item;
        }
    });

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


回答1:


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

let schools = [{
        name: 'YorkTown',
        country: 'Spain'
    },
    {
        name: 'Stanford',
        country: 'USA'
    },
    {
        name: 'Gymnasium Achern',
        country: 'Germany'
    }
];

const editSchoolName = (schools, oldName, name) => {
    return schools.map(item => {
        var temp = Object.assign({}, item);
        if (temp.name === oldName) {
            temp.name = name;
        }
        return temp;
    });
}

var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);
console.log(schools);



回答2:


You need to return the updated object:

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



回答3:


   const editSchoolName = (schools, oldName, newName) =>
    schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));

You could shorten it by using a ternary.




回答4:


If you want to edit only the commented part:

const editSchoolName = (schools, oldName, name) =>
    schools.map(item => {
        if (item.name === oldName) {
          var newItem = Object.assign({},item);
          newItem.name = name;
          return newItem;
        }
        else{
          return item;
        }
    });



回答5:


I wonder how come none of the answers give simple solution

const editSchoolName = (schools, oldName, newName) =>
      schools.map(school => { if (school.name === oldName) school.name = newName;
      return school; 
});



回答6:


let schools = [{
    name: 'YorkTown',
    country: 'Spain'
  },
  {
    name: 'Stanford',
    country: 'USA'
  },
  {
    name: 'Gymnasium Achern',
    country: 'Germany'
  }
];

let updatedSchools = [{
    name: 'New Gen',
    country: 'Spain'
  },
  {
    name: 'Stanford',
    country: 'USA'
  },
  {
    name: 'Gymnasium Achern',
    country: 'Germany'
  }
];

const editSchoolName = ((schools, oldName, name) =>{
  schools.map(item => {
    if (item.name === oldName) {
      item.name = name;
      return item.name;
    } else {
      return item;
    }
  });
  console.log(schools);
});

editSchoolName(schools, 'YorkTown', "New Gen");


来源:https://stackoverflow.com/questions/49627157/update-the-attribute-value-of-an-object-using-the-map-function-in-es6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!