Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values in an array from nested array of objects based on a given condition?

Tags:

lodash

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.

like image 885
Alex Avatar asked May 19 '16 09:05

Alex


People also ask

How do I find the value of a nested object?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

How do you filter nested array of objects?

How do you filter nested array of objects? The filter() method creates a new array with all elements that pass the test implemented by the provided function. Approach 1: This approach uses filter() method to filter the nested object in JavaScript.

Which method is used to convert nested arrays to objects?

toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.

How to access nested array object in JavaScript?

You can access a nested array of objects either using dot notation or bracket notation. JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of an object. Both arrays and objects expose a key -> value structure.


2 Answers

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>
like image 54
ryeballar Avatar answered Oct 26 '22 12:10

ryeballar


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

var result =  _.map(data, item => ({id: item.id, name: item.name}));
like image 30
GoLdO Avatar answered Oct 26 '22 12:10

GoLdO