The Lodash _. unset() method is used to remove the property at the path of the object. If the property is removed then it returns True value otherwise, it returns False.
Overview. The _. remove() method in Lodash removes all the elements from an array that returns a truthy value for the specified predicate. This method will mutate the original array and return an array of all the removed elements.
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
you can do it with _pull
.
_.pull(obj["subTopics"] , {"subTopicId":2, "number":32});
check the reference
You can now use _.reject which allows you to filter based on what you need to get rid of, instead of what you need to keep.
unlike _.pull
or _.remove
that only work on arrays, ._reject
is working on any Collection
obj.subTopics = _.reject(obj.subTopics, (o) => {
return o.number >= 32;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With