75

We have an array of objects as such

var myArr = [ {name: "john", age: 23},
              {name: "john", age: 43},
              {name: "jim", age: 101},
              {name: "bob", age: 67} ];

how do I get the list of objects from myArr where name is john with lodash?

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
sarsnake
  • 26,667
  • 58
  • 180
  • 286

8 Answers8

151

Use lodash _.filter method:

_.filter(collection, [predicate=_.identity])

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

with predicate as custom function

 _.filter(myArr, function(o) { 
    return o.name == 'john'; 
 });

with predicate as part of filtered object (the _.matches iteratee shorthand)

_.filter(myArr, {name: 'john'});

with predicate as [key, value] array (the _.matchesProperty iteratee shorthand.)

_.filter(myArr, ['name', 'John']);

Docs reference: https://lodash.com/docs/4.17.4#filter

Paolo
  • 20,112
  • 21
  • 72
  • 113
Enver Dzhaparoff
  • 4,341
  • 3
  • 19
  • 21
  • 3
    You should mention that your example using _.matchesProperty iteratee shorthand works only from lodash v4. For lodash v3 it's without the array brackets. – NicBright Feb 04 '16 at 06:16
34

Lodash has a "map" function that works just like jQuerys:

var myArr =  [{ name: "john", age:23 },
              { name: "john", age:43 },
              { name: "jimi", age:10 },
              { name: "bobi", age:67 }];

var johns = _.map(myArr, function(o) {
    if (o.name == "john") return o;
});

// Remove undefines from the array
johns = _.without(johns, undefined)
Technotronic
  • 8,424
  • 4
  • 40
  • 53
Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32
15

With lodash:

const myArr = [ {name: "john", age: 23},
                {name: "john", age: 43},
                {name: "jim", age: 101},
                {name: "bob", age: 67} ];

const johnArr = _.filter(myArr, person => person.name === 'john');
console.log(johnArr)

enter image description here

Vanilla JavaScript:

const myArr = [ {name: "john", age: 23},
                {name: "john", age: 43},
                {name: "jim", age: 101},
                {name: "bob", age: 67} ];

const johnArr = myArr.filter(person => person.name === 'john');
console.log(johnArr);

enter image description here

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Yuci
  • 27,235
  • 10
  • 114
  • 113
8

**Filter by name, age ** also, you can use the map function

difference between map and filter

1. map - The map() method creates a new array with the results of calling a function for every array element. The map method allows items in an array to be manipulated to the user’s preference, returning the conclusion of the chosen manipulation in an entirely new array. For example, consider the following array:

2. filter - The filter() method creates an array filled with all array elements that pass a test implemented by the provided function. The filter method is well suited for particular instances where the user must identify certain items in an array that share a common characteristic. For example, consider the following array:

const users = [
    { name: "john", age: 23 },
    { name: "john", age:43 },
    { name: "jim", age: 101 },
    { name: "bob", age: 67 }
];

const user = _.filter(users, {name: 'jim', age: 101});
console.log(user);
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Nver Abgaryan
  • 621
  • 8
  • 13
5
let myArr = [
    { name: "john", age: 23 },
    { name: "john", age: 43 },
    { name: "jim", age: 101 },
    { name: "bob", age: 67 },
];

let list = _.filter(myArr, item => item.name === "john");
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Nver Abgaryan
  • 621
  • 8
  • 13
5

let myArr = [
    { name: "john", age: 23 },
    { name: "john", age: 43 },
    { name: "jim", age: 101 },
    { name: "bob", age: 67 },
];

// this will return old object (myArr) with items named 'john'
let list = _.filter(myArr, item => item.name === 'jhon');

//  this will return new object referenc (new Object) with items named 'john' 
let list = _.map(myArr, item => item.name === 'jhon').filter(item => item.name);
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Nver Abgaryan
  • 621
  • 8
  • 13
1

lodash also has a remove method

var myArr = [
    { name: "john", age: 23 },
    { name: "john", age: 43 },
    { name: "jim", age: 101 },
    { name: "bob", age: 67 }
];

var onlyJohn = myArr.remove( person => { return person.name == "john" })
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
0

Could do something like this to combine filter with an includes to see if the option includes a specific string (answerQuery in this case)

  return filter(answerOptions, (option) => {
    return includes(option.label.toLowerCase(), answerQuery.toLowerCase());
  });
Jeremy
  • 1,170
  • 9
  • 26