I am using lodash to find multiple Objects in an Array.
Here's my current Array when I output JSON.stringify(userList):
[
{"Id":1,"Username":"Usr1","Licenses":["IN","OH"]},
{"Id":2,"Username":"Usr2","Licenses":["IN","FL"]},
{"Id":3,"Username":"Usr3","Licenses":["MI"]}
]
I want to find all Loan Officers who have a License in Indiana.
I am using the lodash library and add the following line of code:
var result = _.find(userList, function (o) { return ~o.Licenses.indexOf(state);});
However, this would only return 1 Object, "Id":2 and NOT BOTH Users with Licneses in Indiana.
How can I have _.find() return me ALL Users with a License in "IN"? Perhaps I need to use a different lodash method and thats fine with me.
Also, if only 1 User is returned back to me, like a User with a Florida license, I need it to return the result as an Array, just like so:
[
{"Id":2,"Username":"Usr2","Licenses":["IN","FL"]}
]
Thank you for your help!
_.findto do what you want. Read the documentation: lodash.com/docs#find where it says_.find"Iterates over elements of collection, returning the first element"