Lets say my object looks like the below. Its a mix of different arrays and parents, no hierarchical order. e.g.
"person": {
"id": 12345,
"name": "John Doe",
"emergencyContacts": [
{
"name": "Jane Doe",
"phone": "888-555-1212",
"relationship": "spouse",
"moreDetails": {
"id": 12345,
"phones": {},
"home": "800-123-4567",
"mobile": "877-123-1234"
}
},
{
"name": "Justin Doe",
"phone": "877-123-1212",
"relationship": "parent",
"mobile": "877-123-1234"
}
],
"workContacts": [
{
"name": "Jane Doe",
"phone": "888-555-1212",
"relationship": "spouse",
"moreworkDetails": {
"id": 12345,
"phones": {},
"home": "800-123-4567",
"mobile": "877-123-1234"
}
},
{
"name": "Justin Doe",
"phone": "877-123-1212",
"relationship": "parent",
"mobile": "877-123-1234"
}
]
}
I want to be able to search the entire object of person
and bring back the value of the key mobile
. I am guessing that the object needs to be flattened and a new array created with the list of "unique" mobile numbers found e.g.
var mobile = {
0: 888-555-1212,
1: 800-123-4567,
2: 877-123-1234,
3: 083-111-3346
}
I have searched for solutions in pure js and lodash, however each one found is knowing what the parent is called and depth of the array - lets assume the parent and depth could be limitless(this sets it aside from question Find by key deep in a nested object). Any help would be muchly appreciated. Thanks