在終極版保存的對象,數組中的替換對象我有一個減速,看起來像這樣一個對象數組:使用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。 –