Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  13] [ 3]  / answers: 1 / hits: 19195  / 7 Years ago, wed, january 3, 2018, 12:00:00

I have an array of objects like:



var a = [
{id: 1, name: 'A'},
{id: 2, name: 'B'},
{id: 3, name: 'C'},
{id: 4, name: 'D'}
];


And Ids array which i want to remove from array a :



var removeItem = [1,2];


I want to remove objects from array a by matching its ids, which removeItem array contains. How can i implement with lodash.



I Checked lodash's _.remove method, but this need a specific condition to remove an item from array. But i have list of ids which i want to remove.


More From » arrays

 Answers
18

As you mentioned you need the _.remove method and the specific condition you mention is whether the removeItem array contains the id of the checked element of the array.



var removeElements = _.remove(a, obj => removeItem.includes(obj.id));
// you only need to assign the result if you want to do something with the removed elements.
// the a variable now holds the remaining array

[#55558] Thursday, December 28, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
renag

Total Points: 22
Total Questions: 97
Total Answers: 95

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
;