Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Javascript create a new array from a nested structure

I have the below JSON;

"level1":[
    {
        "id":"0",
        "index":"0",
        "label":"Level 1 Label a",
        "level2":[
            {
            "id":"0",
            "index":"0",
            "label":"Level 2 Label 1a",
            "level3":[
                {"id":"UniqueId1","index":"0","label":"Level 3 Label 00a"}
            ]
            },
            {
            "id":"1",
            "index":"1",
            "label":"Level 2 Label 1b",
            "level3":[
                {"id":"UniqueId2","index":"0","label":"Level 3 Label 00b"}
            ]
        }]
    },
    {
        "id":"1",
        "index":"1",
        "label":"Level 1 Label b",
        "level2":[
            {
            "id":"0",
            "index":"0",
            "label":"Level 2 Label 2a",
            "level3":[
                {"id":"UniqueId3","index":"0","label":"Level 3 Label a"}
            ]
            },
            {
            "id":"1",
            "index":"1",
            "label":"Level 22 Label",
            "level3":[
                {"id":"UniqueId4","index":"0","label":"Level 3 Label b"}
            ]
        }]
    }
]

My question is if there is a way to create an array of all strings from this of level3.id So given this input, I want the output to be

["UniqueId1", "UniqueId2", "UniqueId3", "UniqueId4"]

I am thinking of using map...but not sure if there is a better way.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The simplest way is using flatMap like this:

level1 = [
        {
            "id": "0",
            "index": "0",
            "label": "Level 1 Label a",
            "level2": [
                {
                    "id": "0",
                    "index": "0",
                    "label": "Level 2 Label 1a",
                    "level3": [
                        { "id": "UniqueId1", "index": "0", "label": "Level 3 Label 00a" }
                    ]
                },
                {
                    "id": "1",
                    "index": "1",
                    "label": "Level 2 Label 1b",
                    "level3": [
                        { "id": "UniqueId2", "index": "0", "label": "Level 3 Label 00b" }
                    ]
                }]
        },
        {
            "id": "1",
            "index": "1",
            "label": "Level 1 Label b",
            "level2": [
                {
                    "id": "0",
                    "index": "0",
                    "label": "Level 2 Label 2a",
                    "level3": [
                        { "id": "UniqueId3", "index": "0", "label": "Level 3 Label a" }
                    ]
                },
                {
                    "id": "1",
                    "index": "1",
                    "label": "Level 22 Label",
                    "level3": [
                        { "id": "UniqueId4", "index": "0", "label": "Level 3 Label b" }
                    ]
                }]
        }
    ]


console.log( level1.flatMap(t=>t.level2).flatMap(t=>t.level3).map(x=>x.id))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!