9

I'm using lodash and I have the following array of objects:

[{
    "id": 1,
    "values": [
        {
            "sub": "fr",
            "name": "foobar1"
        }, 
        {
            "sub": "en",
            "name": "foobar2"
        }
    ]
}, 
{
    "id": 2,
    "values": [
        {
            "sub": "fr",
            "name": "foobar3"
        },
        {
            "sub": "en",
             "name": "foobar4"
        }
    ]
}]

What i'm trying to get the list of ID and name for a given "SUB". So, with the previous object, if I send the sub fr I want to get:

[{
    "id": 1,
    "name": "foobar1"

}, 
{
    "id": 2,
    "name": "foobar3"
}]

Do you know if I can easily do it with lodash?

I tried to use _.pick but it doesn't working(I'm a bit lost with these mixes between nested objects and arrays) _.map(data, function (o) { _.pick(o, ['id', 'values.name']) });.

I also tried to use _.filter with things like _.filter(data, { values: [{ sub: 'fr' }]}); but it return all the items. What I'm looking for is to return the nested part only.

2 Answers 2

10

You can use flatMap() where its callback returns an array of filtered subs using filter() where each filtered item is transformed using map().

var result =  _.flatMap(data, item => 
  _(item.values)
    .filter({ sub: 'fr' })
    .map(v => ({id: item.id, name: v.name}))
    .value()
);

var data = [{
    "id": 1,
    "values": [
        {
            "sub": "fr",
            "name": "foobar1"
        }, 
        {
            "sub": "en",
            "name": "foobar2"
        }
    ]
}, 
{
    "id": 2,
    "values": [
        {
            "sub": "fr",
            "name": "foobar3"
        },
        {
            "sub": "en",
             "name": "foobar4"
        }
    ]
}];

var result =  _.flatMap(data, item => 
  _(item.values)
    .filter({ sub: 'fr' })
    .map(v => ({id: item.id, name: v.name}))
    .value()
);
           
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>

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

1 Comment

Thanks a lot, _.flatMap() seems really useful!
5

following @ryeballar's answer, a shorter version with map

var result =  _.map(data, item => ({id: item.id, name: item.name}));

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.