Home > Net >  typescript: traverse nested array of objects until condition is met
typescript: traverse nested array of objects until condition is met

Time:10-07

I have nested array like following.

  const tree = {
        "id": 1,
        "name": "mainOrgName",
        "children": [
            {
                "id": 10,
                "name": "East Region",
                "children": [
                    {
                        "id": 11,
                        "name": "test east sub 1",
                        "children": [
                            {
                                "id": 12,
                                "name": "sub 22 sub 1",
                                "children": [
                                    {
                                        "id": 15,
                                        "name": "sub 333 of sub ",
                                        "children": [
                                            {
                                                "id": 16,
                                                "name": "sub 4444",
                                                "children": []
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "id": 13,
                "name": "west region",
                "children": [
                    {
                        "id": 14,
                        "name": "test west sub 1",
                        "children": []
                    }
                ]
            }
        ]
    }

I need to traverse through tree.children array to get all id and name of sub arrays and its children until we didn't find children array empty. (Note: children array may be empty or may have further levels)

I need result like follows Expected result

[
    {
        "name": "East Region",
        "value": 10,
        "selected": false
    },
 {
        "name": "test east sub 1",
        "value": 11,
        "selected": false
    },
 {
        "name": "sub 22 sub 1",
        "value": 12,
        "selected": false
    },
 {
        "name": "sub 333 of sub",
        "value": 15,
        "selected": false
    },
 {
        "name": "sub 4444",
        "value": 16,
        "selected": false
    },
    {
        "name": "west region",
        "value": 13,
        "selected": false
    },
 {
        "name": "test west sub 1",
        "value": 14,
        "selected": false
    },
]

I tried following

const candidates = tree.children.map(org => ({name: org.name, value: org.id, selected: false}));

but it gives me following

[
    {
        "name": "East Region",
        "value": 10,
        "selected": false
    },
    {
        "name": "west region",
        "value": 13,
        "selected": false
    }
]

I am trying to get that but not sure how I can put condition that traverse until children is empty and push required fields in final array in required format. May need recursive/call back functions but not sure how I can use that.

Please help to get expected result. Thanks

CodePudding user response:

Try this,

const tree = {
  "id": 1,
  "name": "mainOrgName",
  "children": [
  {
      "id": 10,
      "name": "East Region",
      "children": [{
        "id": 11,
        "name": "test east sub 1",
        "children": [{
          "id": 12,
          "name": "sub 22 sub 1",
          "children": [{
            "id": 15,
            "name": "sub 333 of sub ",
            "children": [{
              "id": 16,
              "name": "sub 4444",
              "children": []
            }]
          }]
        }]
      }]
    },
    {
      "id": 13,
      "name": "west region",
      "children": [{
        "id": 14,
        "name": "test west sub 1",
        "children": []
      }]
    }
  ]
}


let items = []

let result = lootIt(tree.children)
console.log(result)
function lootIt (arr) {
    for(let i = 0 ; i< arr.length ; i  ){
    let obj = {}
    obj['name'] = arr[i]['name']
    obj['value'] = arr[i]['id']
    obj['selected'] = false
    items.push(obj)
    if(arr[i].children !== 0){
    lootIt(arr[i].children)
    }
  }
  
  return items
}

CodePudding user response:

You can use recursion to do it

const tree = {
        "id": 1,
        "name": "mainOrgName",
        "children": [
            {
                "id": 10,
                "name": "East Region",
                "children": [
                    {
                        "id": 11,
                        "name": "test east sub 1",
                        "children": [
                            {
                                "id": 12,
                                "name": "sub 22 sub 1",
                                "children": [
                                    {
                                        "id": 15,
                                        "name": "sub 333 of sub ",
                                        "children": [
                                            {
                                                "id": 16,
                                                "name": "sub 4444",
                                                "children": []
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "id": 13,
                "name": "west region",
                "children": [
                    {
                        "id": 14,
                        "name": "test west sub 1",
                        "children": []
                    }
                ]
            }
        ]
    }

function mapTree(children){
 let result =[]
 for(c of children){
   result.push({name: c.name, value: c.id, selected: false})
   result = result.concat(mapTree(c.children))
 }
 return result
}

console.log(mapTree(tree.children))

  • Related