javascript 根据属性值从 ImmutableJS 列表中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31466400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:51:40  来源:igfitidea点击:

Delete object from ImmutableJS List based upon property value

javascriptnode.jsimmutable.js

提问by user3696212

What would be the simplest way to delete an object from a List based on a value of a property?

根据属性值从 List 中删除对象的最简单方法是什么?

I'm looking for an equivalent of the $pull in MongoDB.

我正在寻找相当于 MongoDB 中的 $pull 。

My List looks simple like this:

我的列表看起来很简单:

[{a: '1' , b: '1'},{a: '2' , b: '2'}]

And I'd like to remove from the array the object with property aset to '1'. In MongoDB, I'd do it like this:

我想从数组中删除属性a设置为“1”的对象。在 MongoDB 中,我会这样做:

Model.update({_id: getCorrectParentObj},{ $pull: {listIDeleteFrom: { a: '1' } } },(err, result)=>{});

How can I get the same result with ImmutableJS?

如何使用 ImmutableJS 获得相同的结果?

回答by okm

You could simply filterthe immutable list:

您可以简单地filter使用不可变列表:

var test = Immutable.List.of(Immutable.Map({a: '1'}), Immutable.Map({a: '2'}));
test = test.filter(function(item) { return item.get('a') !== '1' });

However, filteron non-empty Listwould result a different immutable list, thus you may want to check the occurrence of {a: 1}first:

但是,filter非空List会导致不同的不可变列表,因此您可能需要检查{a: 1}first的出现:

if (test.some(function(item) { return item.get('a') === '1'; })) {
    test = test.filter(function(item) { return item.get('a') !== '1' });
}

回答by Blakes Seven

You don't need Immutable any anything specific for this, just use JavaScript array prototypes:

您不需要 Immutable 任何特定于此的东西,只需使用 JavaScript 数组原型:

var test = [{a: '1' , b: '1'},{a: '2' , b: '2'}];

test.map(function(el,idx) { 
    return ( el.a == "1") ? idx : -1 
} ).filter(function(el) { 
    return el != -1 
}).forEach(function(el) { 
   test.splice(el,1) 
});

Results in:

结果是:

[ { "a" : "2", "b" : "2" } ]

Or you could just get the value from .filter()with a reverse condition:

或者您可以.filter()使用相反的条件从中获取值:

test.filter(function(el) {
    return el.a != 1;
});

Which does not actually affect the array "in place", but you could always "overwrite" with the result.

这实际上并不影响“就地”数组,但您始终可以用结果“覆盖”。

If the testvariable is already an Immutableobject then just convert it with .toArray()first, and re-cast back.

如果test变量已经是一个不可变对象,那么只需.toArray()先将其转换,然后重新转换。

回答by yaya

maybe you can try immutable-data

也许你可以试试不可变数据

var immutableData = require("immutable-data")

var oldArray = [{a: '1' , b: '1'},{a: '2' , b: '2'}]

var data = immutableData(oldArray) 
var immutableArray = data.pick()

//modify immutableArray by ordinary javascript method
var i = 0
immutableArray.forEach(function(item,index){
  if (item.a === '1'){
    immutableArray.splice(index-i,1)
    i++
  }
})

var newArray = immutableArray.valueOf()

console.log(newArray)                    // [ { a: '2', b: '2' } ]
console.log(newArray[0]===oldArray[1])   // true