I am wondering if there is a good way to get nodes within a deeply nested object to announce their position within the object.
For example, if you were to recursively crawl through the example data below (perhaps with help from a library like Lodash), is there a callback function you could pass to each node that would log the location.
Ideally, this would work at any point in the tree, not just leaf nodes.
i.e. announceLocation(data.b.d[2].j.k[1]) //=> ['b', 'd', '2', 'j', 'k', '1']
let data = {
a: 1,
b: {
c: 2,
d: [
{
e: 3,
f: 4
},
{
g: [5, 6, 7],
h: 8,
i: 9
},
{
j: {
k: [10, 11, 12]
}
}
]
},
l: 13
}
UPDATE:
I haven't tried it out yet, but the npm library js-traverse has a contextual value called this.path
that seems to be just what I'm looking for.