最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete object from ImmutableJS List based upon property value - Stack Overflow

programmeradmin4浏览0评论

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

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'}]

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)=>{});

How can I get the same result with ImmutableJS?

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

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'}]

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)=>{});

How can I get the same result with ImmutableJS?

Share Improve this question edited Aug 18, 2016 at 17:34 Mark Amery 155k90 gold badges428 silver badges470 bronze badges asked Jul 16, 2015 at 23:51 user3696212user3696212 3,4395 gold badges20 silver badges36 bronze badges 1
  • Plain js: listIDeleteFrom = listIDeleteFrom.filter(function(item) { return item.a !== 1 }); (not sure about immutable, hence the comment) – tymeJV Commented Jul 17, 2015 at 1:38
Add a comment  | 

3 Answers 3

Reset to default 23

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' });

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' });
}

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

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:

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 test variable is already an Immutable object then just convert it with .toArray() first, and re-cast back.

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
发布评论

评论列表(0)

  1. 暂无评论