Home > Mobile >  How can i get all GUID's in parents and children where enabled = true
How can i get all GUID's in parents and children where enabled = true

Time:02-23

I have a need to get a list of all nodes (guid) in my json doc where the enabled key is set to true. What is the best and most effective way to read all parent nodes as well as possible children. In my case children will always be under the items key and children can also have there own children. I came up with some basic script that will check 2 levels deep but i was hoping there is a better approach which will make this more flrxible where i dont have to hardcode each level.

const enabledguid = []

function getenabled() {
  mydata.jsondoc.forEach(i => {
    if (i.enabled === true) {
      enabledguid.push(i.guid)
    }
    // Check if Parent has children
    if (i.items && i.items.length > 0){
      console.log('we have children '   i.items.length)
        i.items.forEach(i => {
          if (i.enabled === true) {
          enabledguid.push(i.guid)
          }
          if (i.items && i.items.length > 0){
          console.log('Child has children '   i.items.length)
            i.items.forEach(i => {
          if (i.enabled === true) {
          enabledguid.push(i.guid)
          }
            })
          }
    })
  }
  })
  console.log(enabledguid)

  
}

getenabled() 

Here is a sample of a json file

[
    {
        "enabled": true,
        "guid": "F56AAC06-D2EB-4E1C-B84D-25F72973312E",
        "name": "Farms",
        "items": [
            {
                "enabled": true,
                "guid": "144C0989-9938-4AEC-8487-094C23A5F150",
                "name": "New Farm List"
            },
            {
                "enabled": false,
                "guid": "8FBA7B0B-566E-47CD-885B-1C08B57F34F6",
                "name": "Farm Lists"
            },
            {
                "enabled": true,
                "guid": "FCD36DBD-0639-4856-A609-549BB10BEC1A",
                "name": "Farm Upload"
            },
            {
                "enabled": false,
                "guid": "4264DA98-1295-4A65-97C6-313485744B4D",
                "name": "Campaign",
                "items": [
                    {
                        "enabled": false,
                        "guid": "9CBDC6BB-5B3D-4F53-B846-AFE55F34C1E9",
                        "name": "New Campaign"
                    },
                    {
                        "enabled": true,
                        "guid": "281490B5-C67D-4238-9D52-DE1DFA373418",
                        "name": "Campaign List"
                    }
                ]
            }
        ]
    },
    {
        "enabled": true,
        "guid": "1A9127A3-7AC7-4E07-AFA0-B8F8571B1B14",
        "name": "Vendor",
        "items": [
            {
                "enabled": true,
                "guid": "6E6B4DA9-D99D-42D8-A4FE-7C7A82B9F1BE",
                "name": "Vendor List"
            },
            {
                "enabled": false,
                "guid": "63A61762-75FB-466A-8859-25E184C3E016",
                "name": "Add Vendor"
            }
        ]
    }
]

CodePudding user response:

This is a great exercise to learn about recursive functions, look it up.

In this problem the base case will be when you get to an item that does not have a subset of items.

const enabledguid = []

function getenabled(items) {
  items.forEach(item => {
    if (item.enabled === true) {
      enabledguid.push(item.guid)
    }
    if (item.items) {
      getenabled(item.items)
    }
  })
}

getenabled(jsondoc)
console.log(jsondoc)

CodePudding user response:

based on what i found on recursive I came up with this one which does also do the same job.

mydata.filter( item => {
  iratedObject(item)
})


function iratedObject(obj) {
  for (prop in obj){
    if(typeof(obj[prop]) =="object"){
      iratedObject(obj[prop]);
    } else {
      if(obj['enabled'] === true && prop === 'guid'){
        enabledguid.push(obj['guid'])
      }
  }}

}
  • Related