将文件路径列表转换为 ReactJS 中的 JSON。

huangapple go评论67阅读模式
英文:

Convert list of file paths to a json in ReactJS

问题

以下是翻译好的代码部分:

我正在尝试构建一个将路径列表转换为以下对象的函数脚本的输出应该结构如下

输出
```python
data = [
{
    "type": "folder",
    "name": "dir1",
    "children": [
        {
            "type": "folder",
            "name": "photos",
            "children": [
                {
                    "type": "file",
                    "name": "mydir1.pdf",
                },
                {
                    "type": "file",
                    "name": "yourdir1.pdf",
                }
            ]
        }
    ]
}

而输入如下

paths = [
        "dir1/photos/mydir1.pdf",
        "dir1/photos/yourdir1.pdf"
    ]

这是代码的翻译部分,不包括问题或其他内容。

<details>
<summary>英文:</summary>

I am trying to build a function that converts a list of paths into a object as below; the output of the script should be structured as below. 

Output 

data = [
{
"type": "folder",
"name": "dir1",
"children": [
{
"type": "folder",
"name": "photos",
"children": [
{
"type": "file",
"name": "mydir1.pdf",
},
{
"type": "file",
"name": "yourdir1.pdf",
}
]
}
]
}


And input is like this 

paths = [
"dir1/photos/mydir1.pdf",
"dir1/photos/yourdir1.pdf"
]



</details>


# 答案1
**得分**: 1

以下是翻译好的代码部分:

```javascript
function buildTree(paths) {
  const tree = [];
  
  for (let path of paths) {
    const pathParts = path.split('/');
    let subtree = tree;
    
    for (let i = 0; i < pathParts.length; i++) {
      const part = pathParts[i];
      const isFolder = i < pathParts.length - 1;
      
      let node = subtree.find(n => n.name === part);
      
      if (!node) {
        node = {
          type: isFolder ? 'folder' : 'file',
          name: part,
          children: isFolder ? [] : undefined
        };
        
        subtree.push(node);
      }
      
      if (isFolder) {
        subtree = node.children;
      }
    }
  }
  
  return tree;
}
英文:

You can try below function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function buildTree(paths) {
  const tree = [];
  
  for (let path of paths) {
    const pathParts = path.split(&#39;/&#39;);
    let subtree = tree;
    
    for (let i = 0; i &lt; pathParts.length; i++) {
      const part = pathParts[i];
      const isFolder = i &lt; pathParts.length - 1;
      
      let node = subtree.find(n =&gt; n.name === part);
      
      if (!node) {
        node = {
          type: isFolder ? &#39;folder&#39; : &#39;file&#39;,
          name: part,
          children: isFolder ? [] : undefined
        };
        
        subtree.push(node);
      }
      
      if (isFolder) {
        subtree = node.children;
      }
    }
  }
  
  return tree;
}

<!-- end snippet -->

答案2

得分: 0

你可以使用对象引用来构建树。

const final = { result: [] };

for (const path of paths) {
  let context = final;

  for (const name of path.split("/")) {
    if (!context[name]) {
      context[name] = { result: [] };
      let type = "folder";
      if (name.endsWith(".pdf")) {
        type = "file";
      }
      context.result.push({ name, type, children: context[name].result });
    }

    context = context[name];
  }
}

console.log(final.result);

如果你需要进一步的帮助,请随时告诉我。

英文:

You can build tree with the help of object reference.

const final = {result: []};
    
for (const path of paths) {
  let context = final;
  
  for (const name of path.split(&quot;/&quot;)) {
    if (!context[name]) {
      context[name] = { result: [] };
      let type = &quot;folder&quot;;
      if (name.endsWith(&quot;.pdf&quot;)) {
        type = &quot;file&quot;;
      }
      context.result.push({name, type, children: context[name].result})
    }
    
    context = context[name];
  }
}

console.log(final.result)

huangapple
  • 本文由 发表于 2023年2月27日 14:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定