我的操作会将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;
});
示例对象:
{
date: "Sat 2nd",
enjoyments: ['Football', 'Rugby'],
achievements: ['Tennis', 'Football'],
id: 1
},
我希望从数组中删除项目Tennis
,任何想法?
答案 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
。