Home > Enterprise >  Traverse through Javascript Mulitdeminsional Array with Unknown number of Elements
Traverse through Javascript Mulitdeminsional Array with Unknown number of Elements

Time:05-23

Say I have an array that looks as such:

[
{
    "name": "Audiograms",
    "folders": [
        {
            "name": "2022"
        },
        {
            "name": "2021"
        },
        {
            "name": "2020"
        }
    ]
},
{
    "name": "Patient Paperwork"
},
{
    "name": "Repairs"
}
]

And this array can have an infinite amount of objects and subobjects, similar to a file tree.

I have an array letting me know the name of the folders I need to access from the root of the object, like:

["Audiograms", "2022"]

I also do not know this value ahead of time, nor do I know how many items are in this array ahead of time.

How would I be able to actually traverse this file tree using the array of names? I wish to do things like maybe pop the matching object out and move it to another part of the file tree.

Thank you!

CodePudding user response:

OP

"I wish to do things like maybe pop the matching object out and move it to another part of the file tree."

In order to achieve follow-up tasks like the above mentioned one, the next provided solution walks the OP's folder structure and collects for each addressable match an object of two references, target and parent, where the former is the reference of the to be found folder-item, and the latter is the reference of its parent folder-item.

The solution got achieved by a recursively implemented reducer function.

function collectAddressableFolderRecursively(collector, folderItem) {
  const { name = null, folders = [] } = folderItem;
  const {
    address: [parentName, childName], result,
  } = collector;

  if (name === parentName && folders.length) {
    const targetFolder = folders
      .find(({ name }) => name === childName) ?? null;

    if (targetFolder !== null) {
      result.push({
        target: targetFolder,
        parent: folderItem,
      });
    }
  }
  result.push(
    ...folders.reduce(collectAddressableFolderRecursively, {
      address: [parentName, childName],
      result: [],
    })
    .result
  );
  return collector;
}

const folders = [{
  name: 'Audiograms',
  folders: [{
  
    name: '2022',
    folders: [{

      name: 'Audiograms',
      folders: [{
        name: '2022',
      }, {
        name: 'foo',
      }],
    }],
  }, {
    name: '2021',
  }, {
    name: '2020',
  }]
}, {
  name: 'Patient Paperwork',
}, {
  name: 'Repairs',
  folders: [{

    name: 'Audiograms',
    folders: [{
      name: '2022',
    }, {
      name: 'bar',
    }],
  }, {
    name: 'baz',
  }],
}]
const address = ['Audiograms', '2022'];

const { result } = folders
  .reduce(collectAddressableFolderRecursively, {
    address,
    result: [],
  });
console.log({ address, result });
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

If I understood your requirement correctly, You want to filtered out the objects from the input JSON based on the values we have ["Audiograms", "2022"]. It could be at top level or in the folders array. If Yes, Try this :

const jsonObj = [
  {
    "name": "Audiograms",
    "folders": [
      {
        "name": "2022"
      },
      {
        "name": "2021"
      },
      {
        "name": "2020"
      }
    ]
  },
  {
    "name": "Patient Paperwork"
  },
  {
    "name": "Repairs"
  }
];

const searchObj = ["Audiograms", "2022"];

function getFilteredData(jsonObj, searchObj) {
    return jsonObj.filter(obj => {
    return searchObj.includes(obj.name) || obj?.folders?.filter(({ name }) => searchObj.includes(name))
  })
}

console.log(getFilteredData(jsonObj, searchObj));

  • Related