0

Probably a simple question but I can't figure it out. I have an nested object in javascript like this and i have function for find object in object.

[
  {
    "id": "general",
    "components": [
      {
        "id": "na17ea10-e126",
        "parentId": "general",
        "type": "L6",
        "components": [
          {
            "id": "n5c4fd4a-5fe0",
            "parentId": "na17ea10-e126",
            "type": "L6E",
            "components": [
              {
                "id": "n0f17d51-143e",
                "parentId": "n5c4fd4a-5fe0",
                "type": "Text",
                "components": []
              }
            ]
          },
          {
            "id": "n7e86ff5-cfd2",
            "parentId": "na17ea10-e126",
            "type": "L6E",
            "components": []
          }
        ]
      }
    ]
  }
]

and I have a search function

  findInSchema(o, id){
    let i;
    for (i of o) {
      if(i.id==id){
        return i;
      }else if(i.components.length>0){
        return findInSchema(i.components,id);
      }
      
    }
  }

as in the above object example;

findInSchema(obj, 'n5c4fd4a-5fe0')

give me

{
            "id": "n5c4fd4a-5fe0",
            "parentId": "na17ea10-e126",
            "type": "L6E",
            "components": [
              {
                "id": "n0f17d51-143e",
                "parentId": "n5c4fd4a-5fe0",
                "type": "Text",
                "components": []
              }
            ]
          }

but in the other example on the same layer

findInSchema(obj, 'n7e86ff5-cfd2')

give me 'undefined'

2
  • You are basically always terminating the loop when an object has "children", no matter whether an object was found or not. In order words you are always only looking at the first object in the components arrays and never consider the remaining ones. Commented May 6, 2021 at 12:12
  • many excellent ideas here: stackoverflow.com/questions/67399929/… Commented May 6, 2021 at 12:13

1 Answer 1

3

You don't want to terminate the recursive search just because an object has components -- you only want to terminate it if you find what you're looking for within those components.

function findInSchema(o, id) {
  let i;
  for (i of o) {
    if(i.id==id){
      return i;
    } else if(i.components.length>0){
      const inComponents = findInSchema(i.components,id);
      if (typeof(inComponents) != "undefined") return inComponents;
    }
  }
}

const obj = [
  {
    "id": "general",
    "components": [
      {
        "id": "na17ea10-e126",
        "parentId": "general",
        "type": "L6",
        "components": [
          {
            "id": "n5c4fd4a-5fe0",
            "parentId": "na17ea10-e126",
            "type": "L6E",
            "components": [
              {
                "id": "n0f17d51-143e",
                "parentId": "n5c4fd4a-5fe0",
                "type": "Text",
                "components": []
              }
            ]
          },
          {
            "id": "n7e86ff5-cfd2",
            "parentId": "na17ea10-e126",
            "type": "L6E",
            "components": []
          }
        ]
      }
    ]
  }
]

console.log(findInSchema(obj, 'n5c4fd4a-5fe0'));
console.log(findInSchema(obj, 'n7e86ff5-cfd2'));

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

Comments

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.