从键或嵌套对象中的值获取父对象

时间:2020-08-02 16:04:22

标签: javascript json object

我有一个对象,该对象以分层方式包含有关目录的文件夹和文件的所有信息。
例如

{
  "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"
        }
      ]
    }
  ]
}

现在,我将拥有路径值,并且从此信息中,我想访问children属性,该属性与该路径的键值相同。 说,我有路径“ ./parent/child1”,那么我想要相对于此路径的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"
    }
  ]

所以我想知道是否有可能。如果是,那么如何以及如果没有,那么还有其他方法可以达到类似的结果吗?

2 个答案:

答案 0 :(得分:1)

这可以使用递归函数来完成。


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);

答案 1 :(得分:1)

这是另一种无需递归的方法:

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"));

由于data数组不包含有关当前目录(./)的任何信息,因此我从ptha数组中删除了该元素。否则,搜索将允许在任意深度的文件结构中进行搜索。