更新Immutable.Map

时间:2017-01-08 15:26:02

标签: reactjs redux immutable.js

我想将第1项的active更改为true,同时将其他(约100)active更改为false。

import {Map, Record} from 'immutable';

const initialState = new Map({
  0: new Record({
    id: 0,
    name: 'foo',
    active: true,
  }),
  1: new Record({
    id: 1,
    name: 'bar',
    active: false,
  }),
  2: new Record({
    id: 2,
    name: 'baz',
    active: false,
  }),
});

const TARGET = 1;

const newState = initialState.merge(
  initialState.map((value) => {
    if (value.id === TARGET) {
      return value.set('active', true);
    } else if (value.active) {
      return value.set('active', false);
    }
    return value;
  })
);

是否可以做到最简单或更好?谢谢!

0 个答案:

没有答案