Beginner
Beginner

Reputation: 202

Get parent object from key or its value in a nested object

I have an object which contains all information about folders and files of a directory in hierarchical manner.
for example

{
  "path": "./parent",
  "name": "parent",
  "type": "folder",
  "children": [
    {
      "path": "./parent/child1",
      "name": "child1",
      "type": "folder",
      "children": [
        {
          "path": "./parent/child1/file1",
          "name": "file1",
          "size": 651956838,
          "extension": ".pdf",
          "type": "file"
        },
        {
          "path": "./parent/child1/file2",
          "name": "file2",
          "size": 468327031,
          "extension": ".pdf",
          "type": "file"
        }
      ]
    },
    {
      "path": "./parent/child2",
      "name": "child2",
      "type": "folder",
      "children": [
        {
          "path": "./parent/child2/file3",
          "name": "file1",
          "size": 651956838,
          "extension": ".pdf",
          "type": "file"
        },
        {
          "path": "./parent/child2/file4",
          "name": "file2",
          "size": 468327031,
          "extension": ".pdf",
          "type": "file"
        }
      ]
    }
  ]
}

Now, I will have the path value and from this information I want to access the children property, which is sibling to the key of which that path was value of. Say, I have path "./parent/child1", then I want to have the value of children property relative to this path which will be
  [
    {
      "path": "./parent/child1/file1",
      "name": "file1",
      "size": 651956838,
      "extension": ".pdf",
      "type": "file"
    },
    {
      "path": "./parent/child1/file2",
      "name": "file2",
      "size": 468327031,
      "extension": ".pdf",
      "type": "file"
    }
  ]

So I want to know if it is possible or not. If yes then how and if not then is there any other way to achieve similar result?

Upvotes: 0

Views: 1644

Answers (2)

Carsten Massmann
Carsten Massmann

Reputation: 28196

And here is another approach, without recursion:

const data={
  "path": "./parent",
  "name": "parent",
  "type": "folder",
  "children": [
{
  "path": "./parent/child1",
  "name": "child1",
  "type": "folder",
  "children": [
    {
      "path": "./parent/child1/file1",
      "name": "file1",
      "size": 651956838,
      "extension": ".pdf",
      "type": "file"
    },
    {
      "path": "./parent/child1/file2",
      "name": "file2",
      "size": 468327031,
      "extension": ".pdf",
      "type": "file"
    }
  ]
},
{
  "path": "./parent/child2",
  "name": "child2",
  "type": "folder",
  "children": [
    {
      "path": "./parent/child2/file3",
      "name": "file1",
      "size": 651956838,
      "extension": ".pdf",
      "type": "file"
    },
    {
      "path": "./parent/child2/file4",
      "name": "file2",
      "size": 468327031,
      "extension": ".pdf",
      "type": "file"
    }
  ]
}
  ]
};

function getChildrenOf(fs,pth){
  let ptha=pth.split('/');
  ptha.forEach((d,i,a)=>a[i]=(i?a[i-1]+'/':'')+d);
  // console.log(ptha);
  return ptha.filter(d=>d!==".").reduce((a,c)=>
a=a.find(d=>d.path===c && d.type==="folder").children
  ,[fs]);
}

console.log(getChildrenOf(data,"./parent/child1"));

As the data array does not contain any information about the current directory (./) I remove that element from the ptha array. Otherwise the search will allow searches in arbitrary depths of file structures.

Upvotes: 1

Jannes Carpentier
Jannes Carpentier

Reputation: 1898

This can be done using a recursive function.


const paths = {
  "path": "./parent",
  "name": "parent",
  "type": "folder",
  "children": [
    {
      "path": "./parent/child1",
      "name": "child1",
      "type": "folder",
      "children": [
        {
          "path": "./parent/child1/file1",
          "name": "file1",
          "size": 651956838,
          "extension": ".pdf",
          "type": "file"
        },
        {
          "path": "./parent/child1/file2",
          "name": "file2",
          "size": 468327031,
          "extension": ".pdf",
          "type": "file"
        }
      ]
    },
    {
      "path": "./parent/child2",
      "name": "child2",
      "type": "folder",
      "children": [
        {
          "path": "./parent/child2/file3",
          "name": "file1",
          "size": 651956838,
          "extension": ".pdf",
          "type": "file"
        },
        {
          "path": "./parent/child2/file4",
          "name": "file2",
          "size": 468327031,
          "extension": ".pdf",
          "type": "file"
        }
      ]
    }
  ]
}

const pathtofind = "./parent/child1";

function findChildrenInPath(object, path) {
    if (path.startsWith(object.path)) {
        if (object.path == path) {
            return object.children;
        }
        else {
            for (let child of object.children) {
                const result = findChildrenInPath(child, path);
                if (result) {
                    return result;
                }
            }
        }
    }
}

const res = findChildrenInPath(paths, pathtofind);
console.log(res);

Upvotes: 1

Related Questions