23

我正在尝试在 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");

在这里, nameYorkTown应该替换为 name 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功能以实现上述预期结果时需要帮助。

4

7 回答 7

25

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

使用解构

const schools = [
  {
    name: "YorkTown",
    country: "Spain",
  },
  {
    name: "Stanford",
    country: "USA",
  },
  {
    name: "Gymnasium Achern",
    country: "Germany",
  },
];
const editSchoolName = (schools, oldName, newName) =>
  schools.map(({ name, ...school }) => ({
    ...school,
    name: oldName === name ? newName : name,
  }));
const updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);

于 2018-04-03T10:02:53.070 回答
13

您需要返回更新的对象:

const editSchoolName = (schools, oldName, name) =>
  schools.map(item => {
      if (item.name === oldName) {
        return {...item, name};
      } else {
        return item;
      }
});
于 2018-04-03T09:57:55.280 回答
7
   const editSchoolName = (schools, oldName, newName) =>
    schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));

您可以通过使用三元来缩短它。

于 2018-04-03T10:02:52.270 回答
3

如果您只想编辑注释部分:

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;
        }
    });
于 2018-04-03T10:05:58.560 回答
2

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

const editSchoolName = (schools, oldName, newName) =>
      schools.map(school => { if (school.name === oldName) school.name = newName;
      return school; 
});
于 2019-01-09T12:26:39.070 回答
1

就如此容易:

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;
});
于 2021-08-03T08:14:49.217 回答
-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");

于 2018-04-03T10:03:46.923 回答