I'm having trouble narrowing my filter down to the specific object I need in my JSON.
JSON:
var items = {
"itemsGroup": [
{
"item": [
{"description": "Name", "value": "Jane Doe"},
{"description": "Age", "value": 23},
{"description": "Phone", "value": "515-555-1234"},
{"description": "Address", "value": "123 Fake St"},
{"description": "City", "value": "Winchestertonfieldville"},
{"description": "State", "value": "IA"},
{"description": "Zip", "value": 50000}
],
},
{
"item": [
{"description": "Eye Color", "value": "Blue"},
{"description": "Hair", "value": "Blonde"},
{"description": "Height", "value": "5'6"},
{"description": "Weight", "value": 125}
]
}
]
}
I want the object containing Name
in the first nested array:
{"description": "Name", "value": "Jane Doe"}
I can achieve that like this:
_.findWhere(items.itemsGroup[0].item, {description:'Name'})
But I want to account for the case where the JSON could change.
The closest I've gotten is with this underscore function:
_.filter(items.itemsGroup, function(i) {
return _.findWhere(i.item, {description:'Name'});
});
But that returns the entire first nested array rather than the object I'm looking for (as in my first example).
"item": [
{"description": "Name", "value": "Jane Doe"},
{"description": "Age", "value": 23},
{"description": "Phone", "value": "515-555-1234"},
{"description": "Address", "value": "123 Fake St"},
{"description": "City", "value": "Winchestertonfieldville"},
{"description": "State", "value": "IA"},
{"description": "Zip", "value": 50000}
]
What can I try next?