I am 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"
}
]
}]
That I am trying to get a list of ID and name for the given "SUB". So, with the previous object, if I send sub fr, I want to get:
[{
"id": 1,
"name": "foobar1"
},
{
"id": 2,
"name": "foobar3"
}]
You know, can I easily do this with lodash?
I tried using _.pick, but it does not work (I lost these mixes between nested objects and arrays a bit) _.map(data, function (o) { _.pick(o, ['id', 'values.name']) });.
I also tried using _.filterwith things like _.filter(data, { values: [{ sub: 'fr' }]});, but returned all the elements. I am only looking to return the nested part.
source
share