admin管理员组文章数量:1393091
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
1 Answer
Reset to default 8You 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)
本文标签: javascriptRemove multiple items from Immutable List in one shotStack Overflow
版权声明:本文标题:javascript - Remove multiple items from Immutable List in one shot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744738325a2622464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论