更改列表中的一项 In immutable.js
Changing one item in a list In immutable.js
我正在使用 immutable.js,我的数据结构如下:
class ItemList extends Record({
items: new List()
})
我想编写函数来更改此列表中的一项并保持其他项不变。比如一个列表{1, 2, 3, 4},我需要一个函数,如果某一项等于2,就把它改成5。
我正在使用类似
的东西
updateInfo(updatedInfo) {
return this.withMutations(itemList => {
itemList.set('items', list);
});
}
我的问题是在这个函数中,我怎样才能只更新一个项目? if判断放在哪里?
谢谢!
注意:正如另一个答案所提到的,还有未记录的 indexOf
方法,在某些情况下可能更容易使用,只将要查找的值作为参数。
使用findIndex
查找您需要更改的值的索引并set
使用要更改的索引:
list = Immutable.List.of(1, 2, 3, 4);
list = list.set(list.findIndex(function(item) {
return item === 2;
}), 5);
ES6:
list = list.set(list.findIndex((item) => item === 2), 5);
如果你需要旧值来改变它,你可以使用update
而不是像这样设置:
list = list.update(list.findIndex(function(item) {
return item === 2;
}), function(oldValue) {
return 5;
});
ES6:
list = list.update(list.findIndex((item) => item === 2), (oldValue) => 5);
很简单。
list = Immutable.List.of(1, 2, 3, 4);
list = list.set(list.indexOf(2), 5);
console.log(list.get(1)); //5
一个更简洁的版本,基于 forEach。它是一个副作用(改变一个不可变的列表),所以语法类似于使用可变列表 -
var list = Immutable.List.of(1, 2, 3, 4);
// Notice no LHS assignment is required as
// forEach is a side-effect method.
list.forEach((value, index, theList) => {
// You can check either value, or index
if (index === soAndSo
|| value.something === something){
// Just change the value!
value.prop = someNewValue;
// Or, in the above case where value
// is not a reference
theList.set(index) = newValue;
// As we found and changed the value
// of interest, lets exit forEach
return false;
}
});
是的,地图也有一个版本。
我正在使用 immutable.js,我的数据结构如下:
class ItemList extends Record({
items: new List()
})
我想编写函数来更改此列表中的一项并保持其他项不变。比如一个列表{1, 2, 3, 4},我需要一个函数,如果某一项等于2,就把它改成5。
我正在使用类似
的东西updateInfo(updatedInfo) {
return this.withMutations(itemList => {
itemList.set('items', list);
});
}
我的问题是在这个函数中,我怎样才能只更新一个项目? if判断放在哪里?
谢谢!
注意:正如另一个答案所提到的,还有未记录的 indexOf
方法,在某些情况下可能更容易使用,只将要查找的值作为参数。
使用findIndex
查找您需要更改的值的索引并set
使用要更改的索引:
list = Immutable.List.of(1, 2, 3, 4);
list = list.set(list.findIndex(function(item) {
return item === 2;
}), 5);
ES6:
list = list.set(list.findIndex((item) => item === 2), 5);
如果你需要旧值来改变它,你可以使用update
而不是像这样设置:
list = list.update(list.findIndex(function(item) {
return item === 2;
}), function(oldValue) {
return 5;
});
ES6:
list = list.update(list.findIndex((item) => item === 2), (oldValue) => 5);
很简单。
list = Immutable.List.of(1, 2, 3, 4);
list = list.set(list.indexOf(2), 5);
console.log(list.get(1)); //5
一个更简洁的版本,基于 forEach。它是一个副作用(改变一个不可变的列表),所以语法类似于使用可变列表 -
var list = Immutable.List.of(1, 2, 3, 4);
// Notice no LHS assignment is required as
// forEach is a side-effect method.
list.forEach((value, index, theList) => {
// You can check either value, or index
if (index === soAndSo
|| value.something === something){
// Just change the value!
value.prop = someNewValue;
// Or, in the above case where value
// is not a reference
theList.set(index) = newValue;
// As we found and changed the value
// of interest, lets exit forEach
return false;
}
});
是的,地图也有一个版本。