Hey Campers -
Got an interesting challenge going on and am stuck trying to figure out how to save a path to a value in a multidimensional object.
Here is the object:
let newYorkCity = {
'Manhattan': {
'Uptown': {
'Washington Heights': 'Daniel',
'UWS': 'Cathy'
},
'Midtown': {
'Madison Square': 'Susan',
'Theater District': ['Robert', 'Latisha']
},
'Downtown': {
'Tribeca': 'Billy',
'Financial District': {
'Fullstack': {
'11th floor': ['David', 'Nimit'],
'25th floor': 'Ashi'
}
}
}
},
'Brooklyn': {
'Bushwick': 'Marilyn',
'Bed-Stuy': ['Juan', 'Denice']
},
'Queens': {
'Astoria': 'Ella',
'Flushing': 'Eric'
},
'Bronx': {
'Fordham': 'Aaron',
'Melrose': 'Krysten'
},
'Staten Island': {
'Arlington': ['Nadine', 'Mose'],
'Elm Park': 'Arthur'
}
};
Now, Iāve written code already that searches through the objects and finds a specific person, but Iām having trouble figuring out how to save the specific path to the person. For example: when I search for āNimitā, I want to return the path: [āManhattanā, āDowntownā, āFinancial Districtā, āFullstackā, ā11th floorā]
Currently, I am pushing the path to an array as I do the search but Iām ending up with an array like this:
[ 'Manhattan',
'Uptown',
'Midtown',
'Theater District',
'Downtown',
'Financial District',
'Fullstack',
'11th floor' ]
As you can see, the array is saving extra paths it searches down that I donāt need. It saves Uptown, Midtown, etc., which are paths the program searches but are not needed for the final array.
Iām having a lot of difficulty trying to figure out a way to āfilterā or āsortā this properly. Iāve been trying to figure out a way to push one item onto the array at a time and then delete that item, but it ends up with only the first and last item, since thereās no way I know of to setup a conditional that saves one item from each branch.
This code is also dynamic so I canāt simply hard-code this line in, the object isnāt always going to be New York City and the keys names in the object are going to change. Iām using recursion on this, which works well, however without being able to setup a proper conditional statement to āsaveā only the keys I want - Iām getting stuck.
Any suggestions or if anyone has any code that can do what Iām asking, that would be appreciated. This is not an FCC challenge, so you can post answers here.
Thanks.