2

I found this solution:

var collections = [
    {name: 'somename1',  desctiption: 'xyz'}, 
    {name: 'somename',   desctiption: 'ds'},
    {name: 'somename',   desctiption: 'rtrt'},
    {name: 'somename2',  desctiption: 'nhf'},
    {name: 'somename1',  desctiption: 'qwe'}
];

names = ['somename1', 'somename2']

_(collections)
    .keyBy('name')
    .at(names)
    .value();

But the result contains only one value with somename1, how can I get all values with somename1?

2 Answers 2

9

If by all values you mean the objects, then

const values = collections.filter(e => ['somename1', 'somename2'].includes(e.name))

If you mean the description, then

const descriptions = values.map(e => e.description)

For this kind of stuff you don't really need lodash anymore.

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

Comments

0

You can use find method to get one value

_.find(collections,{name : 'somename1'})

For all objects with somename1

_.pullAllWith(collections,{ name : 'somename1'})

Console.log the results are as expected

1 Comment

I need find by an array of values: names = ['somename1', 'somename2']

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.