array.filter不从数组中删除值

时间:2017-05-31 20:49:23

标签: javascript reactjs

我的操作会将id,text和type发送到我的reducer,但我的achievements数组保持不变:

case REMOVE_ACHIEVEMENT:
  return state.map(item => {
      if (item.id === action.id) {
          return Object.assign({}, item, {
            achievements: [
            ...item.achievements.filter(achievement => achievement !== action.id.text)
            ]
          });
      }

    return item;
});

enter image description here

示例对象:

  {
    date: "Sat 2nd",
    enjoyments: ['Football', 'Rugby'],
    achievements: ['Tennis', 'Football'],
    id: 1
  },

我希望从数组中删除项目Tennis,任何想法?

1 个答案:

答案 0 :(得分:2)

case REMOVE_ACHIEVEMENT:
  return state.map(item => {
      if (item.id === action.id) {
          return Object.assign({}, item, {
            achievements: item.achievements.filter(val => val !== action.id.text)
          });
      }

    return item;
});

无需使用扩展运算符,因为过滤器将返回没有过滤项的新数组。

修改 正如@mhodges指出正确的描述方式是spread syntax而不是spread operator