在终极版保存的对象,数组中的替换对象我有一个减速,看起来像这样一个对象数组:使用Javascript/Lodash
[
{id:1, name:Mark, email:[email protected]},
{id:2, name:Paul, email:[email protected]},
{id:3,name:sally, email:[email protected]}
]
下面是我的减速器。到目前为止,我可以通过下面的currentPeople
减速机添加一个新的对象:
const INITIAL_STATE = { currentPeople:[]};
export default function(state = INITIAL_STATE, action) {
switch (action.type) {
case ADD_PERSON:
return {...state, currentPeople: [ ...state.currentPeople, action.payload]};
}
return state;
}
但这里是我卡住了。我可以使用lodash通过减速器更新一个人吗? 如果我发了行动有效载荷是这样的:
{id:1, name:Eric, email:[email protected]}
我将能够以1与新领域的标识更换对象?
我选择这个作为我仍然需要这种格式和它的作品答案!只是一个简短的问题,我用你的映射的例子,它的工作原理是完美的,但你会建议使用lodash,因为这需要浏览器的支持? – lost9123193
这确实取决于你需要支持哪些浏览器。 Array.map支持IE9 +以及Chrome/SafariFF/Opera。所以,除非你有特定的浏览器要求支持IE8,否则你应该对array.map很好。要获得完整的浏览器支持和旧版浏览器的polyfill,请参阅MDN页面:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map。 –