I am looking at the lodash documentation for remove()
and I am not sure how to use it.
Say I have an array of Friends,
[{ friend_id: 3, friend_name: 'Jim' }, { friend_id: 14, friend_name: 'Selma' }]
How do you remove friend_id: 14
from the array of Friends?
Remove uses a predicate function. See example:
var friends = [{ friend_id: 3, friend_name: 'Jim' }, { friend_id: 14, friend_name: 'Selma' }]; _.remove(friends, friend => friend.friend_id === 14); console.log(friends); // prints [{"friend_id":3,"friend_name":"Jim"}]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
You can use filter.
var myArray = [1, 2, 3];
var oneAndThree = _.filter(myArray, function(x) { return x !== 2; });
console.log(allButThisOne); // Should contain 1 and 3.
Edited: For your specific code, use this:
friends = _.filter(friends, function (f) { return f.friend_id !== 14; });
You can also do it like this
list.splice(_.findKey(this.list, function(e) {
return e.id === 12;
}), 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.