根据属性值从ImmutableJS列表中删除对象 [英] Delete object from ImmutableJS List based upon property value
问题描述
基于属性值从列表中删除对象的最简单方法是什么?
What would be the simplest way to delete an object from a List based on a value of a property?
我正在寻找与MongoDB中的$ pull等效的东西.
I'm looking for an equivalent of the $pull in MongoDB.
我的列表看起来很简单:
My List looks simple like this:
[{a: '1' , b: '1'},{a: '2' , b: '2'}]
我想从数组中删除属性 a 设置为"1"的对象.在MongoDB中,我会这样:
And I'd like to remove from the array the object with property a set to '1'. In MongoDB, I'd do it like this:
Model.update({_id: getCorrectParentObj},{ $pull: {listIDeleteFrom: { a: '1' } } },(err, result)=>{});
如何使用ImmutableJS获得相同的结果?
How can I get the same result with ImmutableJS?
推荐答案
You could simply filter
the immutable list:
var test = Immutable.List.of(Immutable.Map({a: '1'}), Immutable.Map({a: '2'}));
test = test.filter(function(item) { return item.get('a') !== '1' });
但是,非空List
上的filter
会导致不同的不可变列表,因此您可能要首先检查{a: 1}
的出现:
However, filter
on non-empty List
would result a different immutable list, thus you may want to check the occurrence of {a: 1}
first:
if (test.some(function(item) { return item.get('a') === '1'; })) {
test = test.filter(function(item) { return item.get('a') !== '1' });
}
这篇关于根据属性值从ImmutableJS列表中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!