0

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!

1
  • You can't use _.find to do what you want. Read the documentation: lodash.com/docs#find where it says _.find "Iterates over elements of collection, returning the first element" Commented May 10, 2016 at 14:04

5 Answers 5

1

you can use _.filter - which returns all matched items

Sign up to request clarification or add additional context in comments.

Comments

0

lodash's method _.filter is a way to go:

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

_.find returns only first matched element

Comments

0

You may do as follows. It will get you all objects with searched state. You can search for multiple states as well.

myData = [
          {"Id":1,"Username":"Usr1","Licenses":["IN","OH"]},
          {"Id":2,"Username":"Usr2","Licenses":["IN","FL"]},
          {"Id":3,"Username":"Usr3","Licenses":["MI"]}
         ],
getAll = (...sts) => myData.reduce((p,o) => sts.some( s => o.Licenses.includes(s)) ? p.concat(o) : p ,[]);

document.write("<pre>" + JSON.stringify(getAll("IN"),null,2) + "</pre>");

Comments

0

You can use array's filter function without using lodash

So one liner would be

var state = 'IN';
var test = [
   {"Id":1,"Username":"Usr1","Licenses":["IN","OH"]},
   {"Id":2,"Username":"Usr2","Licenses":["IN","FL"]},
   {"Id":3,"Username":"Usr3","Licenses":["MI"]}
]

test.filter(x=>x.Licenses.includes(state))

Comments

0

There is one more way to do it using chain, filter and includes

var users = [
   {"Id":1,"Username":"Usr1","Licenses":["IN","OH"]},
   {"Id":2,"Username":"Usr2","Licenses":["IN","FL"]},
   {"Id":3,"Username":"Usr3","Licenses":["MI"]}
];

var ret = _.chain(users).filter(function(o){
         return _.includes(o.Licenses,'IN');
    }).value();

console.log(ret);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.