Home > Software engineering >  Get path to object in nested array
Get path to object in nested array

Time:01-03

I have the following array:

[
  {
    name: 'images',
    children: [
      {
        name: 'layer-2',
        children: [
          {
            name: 'image-2.png'
          },
          {
            name: 'image-3.png'
          }
        ],
      },
      {
        name: 'image-1.png'
      }
    ],
  },
  {
    name: 'videos',
    children: [
      {
        name: 'video-1.mp4'
      },
      {
        name: 'layer-2',
        children: ...
      }
    ]
  }
]

There can be up to 10 layers. The output of the function I want to write should be: 'images/layer-2/image-2.png' if I am looking for image-2.png

I have absolutly no clue how I have to write this recursive function. Can somebody help me?

Thanks in advance!

CodePudding user response:

I'm assuming you want to find the very first instance of what your are looking for and only that. In this instance, the recursive function will go through an item and its children until it finds what it's looking for.

We need a function that goes through an array of objects and if one of them has an array of children, the same function will then go through the array of those children. If the target was not found, function returns null. If it was found, it returns the result.

I hope this clears it up looking at the console output!

const data = [
  {
    name: 'cats',
    children: [
      {
        name: 'catchild1',
        children: [
          { name: 'catgrandchild1' },
          { name: 'catgrandchild2' }
        ],
      },
      { name: 'catchild1' }
    ],
  },
  {
    name: 'dogs',
    children: [
      { name: 'dogchild1' },
      {
        name: 'dogchild2',
        children: { name: 'doggrandchild1' }
      }
    ]
  }
];
  
function findRecursive(nameToFind, arrayToSearch) {
    // We are searching through an array of objects
    // The objects always have a name 
    // The objects sometimes have array of similar child objects
    // We are looking for a specific name
    for (let i = 0; i < arrayToSearch.length; i  ) {
        const object = arrayToSearch[i]

        console.log("We are looking at "   object.name)

        // If this is the object we are looking for stop the loop and return
        if (object.name === nameToFind) {
          console.log("FOUND IT!")
          return object
        }

        // If this object has children, loop over them recursively
        if (object.children) {
            const result = findRecursive(nameToFind, object.children)
            // If the thing we are searching for was found, return it
            if (result) { return result }
        }

        // If we get this far, the nameToFind was not found
        // Meaning the object we are looking at is not the one
        // AND none of it's descendents are either
        // Move on to the next object in array we are looking at
    }

    return null
}

console.log(findRecursive("dogchild2", data))

  • Related