我想将第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;
})
);
是否可以做到最简单或更好?谢谢!