简体   繁体   English

使用 ES6 中的 map 函数更新对象的属性值

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

I am trying to code this in ES6.我正在尝试在 ES6 中对此进行编码。 Below is what I am trying to achieve.以下是我正在努力实现的目标。 Let's say I have an array of objects called schools .假设我有一个名为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 .现在,我想编写一个名为editSchoolName的函数,它将接受 3 个参数, schools (这是我在上面定义的数组)、 oldNamename

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 .我将在参数oldName传递学校的名称,该名称应使用参数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.我不想更改变量schools的状态,所以我使用了一个map函数,它将返回一个包含更改的新数组。

The editSchoolName function will be called like this - editSchoolName函数将像这样被调用 -

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

Here, the name YorkTown should be replaced with the name New Gen .此处,名称YorkTown应替换为名称New Gen So the expected value of the array updatedSchools should be -所以数组updatedSchools的期望值应该是 -

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

This is how my editSchoolName function looks like -这就是我的 editSchoolName 函数的样子 -

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.需要帮助在editSchoolName函数中进行更改以实现上述预期结果。

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;
      }
});
   const editSchoolName = (schools, oldName, newName) =>
    schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));

You could shorten it by using a ternary. 您可以使用三元缩短它。

try this, ES6 Object.assign() to create copy of array element and update new object. 试试这个,ES6 Object.assign()来创建数组元素的副本并更新新对象。

 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); 

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;
        }
    });

as simple as that:就如此容易:

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

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; 
});

 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"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM