1

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.

0

1 Answer 1

2

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))

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks...I am really not aware of flatMap...How is the modern browser support for this ?
@copenndthagen You're welcome. The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. So when you need to go for two level you need to use them twice and then you can get any property you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.