具有多种变化的传播属性:单线性ecma不变性

时间:2017-02-08 10:43:00

标签: javascript redux immutability ecmascript-next

我刚刚改变了这个

const newCities = {...newState.cities};
newCities[action.payload.id] = action.payload;
newState.cities = newCities;

到单行

newState.cities = {...newState.cities, [action.payload.id]: action.payload};

但我该如何更改

const newCities = {...newState.cities};
action.payload.forEach(city=> newCities[city.id] = city);
newState.cities = newCities;

到单线?东西〜=

newState.cities = {...newState.cities, action.payload.map(city=> [city.id]: city)};

1 个答案:

答案 0 :(得分:1)

最终解决方案:

News.category(params['categori']).tag(params['tag_list'])

以前我忘了初始化reduce array =)

旧方法: 可能是这样吗?

newState.cities = {...newState.cities, ...action.payload.reduce((o,a)=> ({...o,[a.id]:a}),{})};

<强>更新

newState.cities = {...newState.cities, ...action.payload.map(city=> ({[city.id]: city}) )};

更新(完整丑陋):

newState.cities = {...newState.cities, ...action.payload.reduce(city=> ({[city.id]:city}) )};