How can I remove an element from a list, with lodash?
As lyyons pointed out in the comments, more idiomatic and lodashy way to do this would be to use _.remove
, like this
_.remove(obj.subTopics, {
subTopicId: stToDelete
});
Apart from that, you can pass a predicate function whose result will be used to determine if the current element has to be removed or not.
_.remove(obj.subTopics, function(currentObject) {
return currentObject.subTopicId === stToDelete;
});
Alternatively, you can create a new array by filtering the old one with _.filter
and assign it to the same object, like this
obj.subTopics = _.filter(obj.subTopics, function(currentObject) {
return currentObject.subTopicId !== stToDelete;
});
Or
obj.subTopics = _.filter(obj.subTopics, {subTopicId: stToKeep});
Just use vanilla JS. You can use splice
to remove the element:
obj.subTopics.splice(1, 1);
Demo