I'm looking for a couple of different ways to access the data below, using ES6 and/or lodash.
What I'm trying to do is to return the parent object, where device_id matches.
i.e, for each item in entities, if in any of the nested objects, the device_id field = abc, return the whole array item
const entities = [
{
"<unknown key>": {
"entity_id": "something",
"device_id": "abc"
},
"<unknown key>": {
"entity_id": "else",
"device_id": "abc"
},
},
{
"<unknown key>": {
"entity_id": "hello",
"device_id": "xyz"
},
"<unknown key>": {
"entity_id": "world",
"device_id": "xyz"
}
}
]
Use
Array::find()to find the item with matching a child with the given device_id:If you are interested in speed, use
for..ininstead ofObject.values().some(). That way you avoid an intermediate array: