I'm trying to iterate through a nested object to retrieve a specific object identified by a string. In the sample object below, the identifier string is the "label" property. I can't wrap my head around how to iterate down through the tree to return the appropriate object. Any help or suggestions would be greatly appreciated.
var cars =
{
label: 'Autos',
subs:
[
{
label: 'SUVs',
subs: []
},
{
label: 'Trucks',
subs: [
{
label: '2 Wheel Drive',
subs: []
},
{
label: '4 Wheel Drive',
subs: [
{
label: 'Ford',
subs: []
},
{
label: 'Chevrolet',
subs: []
}
]
}
]
},
{
label: 'Sedan',
subs: []
}
]
}
modify from Peter Olson's answer: https://stackoverflow.com/a/8085118
!obj || (typeof obj === 'string'
I made a pick method like lodash pick. It is not exactly good like lodash _.pick, but you can pick any property event any nested property.
for example:
Code :
and here is the link to live example with unit tests