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

javascript - Remove multiple items from Immutable List in one shot - Stack Overflow

programmeradmin1浏览0评论

Given this:

state = Immutable.fromJS({
  selectedTrackIds: ['foo', 'bar', 'baz'],
});

Is there a way to get a new state where 'foo' and 'baz' are removed from selectedTrackIds, using a single statement (using only Immutable and plain JS)? Or will I just have to use lodash?

return state.set('selectedTrackIds', Immutable.fromJS(_.difference(
  state.get('selectedTrackIds').toJSON(), ['foo', 'baz']
)));

Given this:

state = Immutable.fromJS({
  selectedTrackIds: ['foo', 'bar', 'baz'],
});

Is there a way to get a new state where 'foo' and 'baz' are removed from selectedTrackIds, using a single statement (using only Immutable and plain JS)? Or will I just have to use lodash?

return state.set('selectedTrackIds', Immutable.fromJS(_.difference(
  state.get('selectedTrackIds').toJSON(), ['foo', 'baz']
)));
Share Improve this question asked Apr 6, 2016 at 7:48 ffxsamffxsam 27.8k34 gold badges100 silver badges150 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use filter to remove the items you don't want:

return state.set('selectedTrackIds',
  state.get('selectedTrackIds').filter(function(x) {
    return ['foo', 'baz'].indexOf(x) < 0; // false return value => remove from list
  })
);

Or bine it with map, and some ES6 syntax:

state.map(x => x.filter(y => ['foo', 'baz'].indexOf(y) < 0))

(filter and map are standard JS, and Immutable provides its own implementations that work directly with Immutable collections)

发布评论

评论列表(0)

  1. 暂无评论