0

I have a collection,

 var users = [
       {'user': 'Mike', 'age': 11,  'description': 'Null'},
       {'user': 'Kiddo', 'age': 36,  'description': 'Not Available'},
       { 'user': 'Jack', 'age': 36,  'description': 'test'}
     ];

How can use lodash library to remove the entries which has 'description' :'Not Available' ?

Edit 1 : Tried using the below ,

function removeItemsByAttrValue (collection, attrValue, matchValue) { 
    return _.filter(collection, val => val.attrValue !== matchValue);
}

Invoked it like,

 removeItemsByAttrValue (users, 'description', 'Not Available');

Somehow, this didn't filter the item.

1 Answers1

3

You can use the built in JS method for arrays filter

var filteredUsers = users.filter(val => val.description !== 'Not Available');

Lodash also has a filter method, but it's unnecessary to use here

_.filter(users, val => val.description !== 'Not Available') 
pizzarob
  • 11,711
  • 6
  • 48
  • 69
  • Thanks, when I used it inside a function and passed the params, somehow it doesn't filter. I edited the question with what I tried. But if the filter is used as it is it works. –  Apr 11 '17 at 17:45
  • You would need to update your function to look like this: `_.filter(collection, val => val[attrValue] !== matchValue);` notice the bracket notation @Vishnu – pizzarob Apr 11 '17 at 20:03
  • Thanks again, I will try it out! –  Apr 11 '17 at 20:07