Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a nested object from Array javascript

I have the array like this\

[
 "/server/api/v2.0/terms-conditions/latest",
 "/server/api/v2.0/tools/extension/rewrite",
 "/server/api/v2.0/users/presigned-url",
 "/server/api/v2.0/users/detail",
 "/server/api/v2.0/users/detail",
 "/ai/fingerprint",
 "/ai/fingerprint",
 "/server/api/v2.0/tools/web-tools/product-description-generator",
 "/analytics/api/v2.0/user-stats/analytic-clarity",
 "/server/api/v2.0/tools/generate/readability-score-1",
 "/server/api/v2.0/tools/generate/readability-score-2",
 "/analytics/api/v2.0/user-stats/analytic-article",
]

and I want an output like\

{
"server":{
      "api":{
         "v2.0":{
            "terms-condition":{
               "latest":[
                  "/server/api-1/v2.0/terms-condition/latest""m"
               ]
            },
            "tools":{
               "web-tools":{
                  "product-description-generator":[
                     "/server/api/v2.0/tools/web-tools/product-description-generator"
                  ]
               },
               "generate":{
                  "readability-score-1":[
                     "/server/api/v2.0/tools/generate/readability-score-1"
                  ],
                  "readability-score-2":[
                     "/server/api/v2.0/tools/generate/readability-score-2"
                  ]
               },
               "extension":{
                  "rewrite":[
                     "/server/api/v2.0/tools/extension/rewrite"
                  ]
               }
            },
            "users":{
               "presigned-url":[
                  "/server/api/v2.0/users/presigned-url"
               ],
               "detail":[
                  "/server/api/v2.0/users/detail",
                  "/server/api/v2.0/users/detail"
               ]
            }
         }
      }
   },
   "analytics":{
      "api":{
         "v2.0":{
            "user-stats":{
               "analytic-clarity":[
                  "/analytics/api/v2.0/user-stats/analytic-clarity"
               ],
               "analytic-article":[
                  "/analytics/api/v2.0/user-stats/analytic-article"
               ]
            }
         }
      }
   },
   "ai":{
      "fingerprint":[
         "/ai/fingerprint",
         "/ai/fingerprint"
      ]
   }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const data = [
  "/server/api/v2.0/terms-conditions/latest",
  "/server/api/v2.0/tools/extension/rewrite",
  "/server/api/v2.0/users/presigned-url",
  "/server/api/v2.0/users/detail",
  "/server/api/v2.0/users/detail",
  "/ai/fingerprint",
  "/ai/fingerprint",
  "/server/api/v2.0/tools/web-tools/product-description-generator",
  "/analytics/api/v2.0/user-stats/analytic-clarity",
  "/server/api/v2.0/tools/generate/readability-score-1",
  "/server/api/v2.0/tools/generate/readability-score-2",
  "/analytics/api/v2.0/user-stats/analytic-article",
]

// create object
const obj = {}

// iterate over each path
for (const path of data) {
  // split by / without the first /
  const parts = path.slice(1).split('/')
  // get last element
  const last = parts.pop()
  let current = obj
  // iterate over parts
  for (const part of parts) {
    // if not object yet, create one
    if (!current[part]) {
      current[part] = {}
    }
    current = current[part]
  }
  // add the most nested array with full path
  current[last] = [path]
}

console.log(obj)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading