Skip to content
Advertisement

How to iterate through a JavaScript object and get parent properties?

I’m iterating through a JS object (obj) and would like to retrieve the parent property of a property. E.g. if k is ‘v9’. Something like k.parent would return ‘c’ and k.parent.parent would return ‘b’. Of course, the syntax is different and that’s what I’m looking for

JavaScript

Advertisement

Answer

an idea can be to store the parent key as a parameter of iterate function

JavaScript

Another idea can be to stored a structure fill at each iterate step if there is a parent to the current object use recursive behavior to fill it

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement