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

时间:2018-04-03 09:55:15

标签: javascript arrays ecmascript-6 es6-map

我正在尝试在ES6中编写代码。以下是我想要实现的目标。假设我有一个名为schools的对象数组。

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

现在,我想编写一个名为editSchoolName的函数,该函数将采用3个参数,schools(我在上面定义的数组),oldNamename

我将在参数oldName中传递学校的名称,并且应使用参数name中的值更新该名称。

我不想更改变量schools的状态,因此我使用的是map函数,该函数将返回带有更改的新数组。

editSchoolName函数将像这样调用 -

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

此处,名称YorkTown应替换为名称New Gen。所以数组updatedSchools的期望值应为 -

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

这就是我的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;
        }
    });

editSchoolName函数中进行更改以获得上述所需结果时需要帮助。

7 个答案:

答案 0 :(得分:2)

您需要返回更新的对象:

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

答案 1 :(得分:2)

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

您可以使用三元缩短它。

答案 2 :(得分:1)

试试这个,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);




答案 3 :(得分:1)

如果您只想编辑评论的部分:

    ArrayList myList;
    myList = new ArrayList();
    myList.add("abc"); // “unchecked call to add(E) as a member of the raw type java.util.ArrayList
    Integer s = (Integer) myList.get(0);

答案 4 :(得分:0)

我想知道为什么没有答案给出简单的解决方案

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

答案 5 :(得分:0)

就这么简单:

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

答案 6 :(得分:-1)

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