immutable.js的文档,缺少描述性示例。有人可以解释一下,如何在ImmutableJS中执行以下操作:
function isOdd (v) {
return v % 2 === 0
}
var collection = [{a: 1, b: 2}, {a: 3, b: 7}, {a: 5, b: 6}];
collection.map(item => {
if (isOdd(item.b)) {
item.a = item.a * 2;
}
return item;
})
非常感谢您的帮助。
const collection = Immutable.fromJS([{a: 1, b: 2}, {a: 3, b: 7}, {a: 5, b: 6}]);
const isOdd = a => b => b % 2 ? a : a * 2;
collection
.map(item => item
.update('a', a => isOdd(a)(item.get('b'))))
所以这是我的版本:
const collection = Immutable.fromJS([{a: 1, b: 2}, {a: 3, b: 7}, {a: 5, b: 6}]);
console.log(
collection
.map(item => {
if (item.get('b') % 2 == 0) {
return item.set('a', item.get('a') * 2)
} else {
return item;
}
}).toJS()
);