2

Given a JavaScript object:

const websiteTree = { 
    home: {
       about: {},
       categoryA: {
           postA: {}
       }
    }
}

and a path:

websiteTree.home.categoryA.postA

how could I get the nested path as "string", example:

console.log(getUrlPath(websiteTree.categoryA.postA)) // expected output: "/categoryA/postA"

I need a data structure to represent URLs in JavaScript. No idea how I can do this.

Émerson Felinto
  • 433
  • 7
  • 18
  • Does this answer your question? [Javascript reflection: Get nested objects path](https://stackoverflow.com/questions/32141291/javascript-reflection-get-nested-objects-path) – ShinaBR2 Oct 21 '21 at 05:59
  • I adapted the code based on my goals. Unfortunately I can't use the same approach as him to solve this problem, as I need to pass the entire object path that I would like to get the path as a string. I created a minimal verifiable example so you can better understand the problem. Please see the last two "console.log" at the end of this repl.it. https://replit.com/@felintodev/UnfitBlindMonotone#index.js – Émerson Felinto Oct 21 '21 at 06:56

0 Answers0