TransWikia.com

lodash find returns only one result

Stack Overflow Asked by manuelBetancurt on January 31, 2021

I’m using lodash find, and on my test, it is only returning one result, is this the expected response? how to find all instances?

var users = [
    { firstName: "John", lastName: "Doe", age: 28, gender: "male" },
    { firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
    { firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
    { firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
  ];

  var encontre = _.find(users, { lastName: "Doe" })

  console.log("usuario encontre::", encontre)

response

usuario encontre:: { firstName: ‘John’, lastName: ‘Doe’, age: 28,
gender: ‘male’ }

so how to seethe 2 users with lastName: Doe?

thanks

One Answer

Try with _.filter as _.find returns the first matched element. _.filter will return an array of all matched elements.

var users = [
    { firstName: "John", lastName: "Doe", age: 28, gender: "male" },
    { firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
    { firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
    { firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
  ];

  var encontre = _.filter(users, { lastName: "Doe" })

  console.log("usuario encontre::", encontre)

jsfiddle for ref : https://jsfiddle.net/c_Dhananjay/b6ngxhvp/

Answered by Dhananjay C on January 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP