从目录路径中以字符串形式获取对象数组中的目录路径

时间:2018-06-21 21:09:48

标签: javascript arrays recursion

曾经搜索过这样的帖子,但现在还没有任何答案。

我有一个类似这样的文件夹数组:

[{
"name": "home",
"folders": [{
    "name": "New Folder",
    "folders": [{
        "name": "53w5r",
        "folders": [{
            "name": "test",
            "folders": []
        }]
    }]
}, {
    "name": "public folder",
    "folders": [{
        "name": "cold",
        "folders": []
    }, {
        "name": "hot",
        "folders": []
    }]
}, {
    "name": "My Folder",
    "folders": []
}]

}]

我需要使用如下路径字符串来搜索此数组:

"home,New Folder,53w5r,test"

我正在搜索该文件夹,因此findFolder方法将使用上面的字符串并在“ test”文件夹中返回文件夹对象。

3 个答案:

答案 0 :(得分:3)

假设您的逗号分隔路径始终有效,则可以使用以下简单方法遍历数据:

var str = "home,New Folder,53w5r,test";
var data = [{
  "name": "home",
  "folders": [{
    "name": "New Folder",
    "folders": [{
      "name": "53w5r",
      "folders": [{
        "name": "test",
        "folders": []
      }]
    }]
  }, {
    "name": "public folder",
    "folders": [{
      "name": "cold",
      "folders": []
    }, {
      "name": "hot",
      "folders": []
    }]
  }, {
    "name": "My Folder",
    "folders": []
  }]
}];

var folderNames = str.split(",");
var result = folderNames.reduce((results, folderName, index) => {
  var currFolder = results.find(folder => folder.name === folderName);
  // if last folder, return entire folder, otherwise return currFolder.folders
  return index === folderNames.length-1 ? currFolder : currFolder.folders;
}, data);
console.log(result);

答案 1 :(得分:2)

这是我没有实现的实现,如果找不到目录,它将返回null。

const dirSeperator = ",";

function findDir(path, dirs) {
  var parts = path.split(dirSeperator);

  if (parts && parts.length > 0) {
    var n = parts[0];
    for (var i = 0; i < dirs.length; i++) {
      if (dirs[i].name === n) {
        if (parts.length > 1)
          return findDir(path.replace(n + dirSeperator, ''), dirs[i].folders)
        else if (parts.length == 1)
          return dirs[i]
      }
    }

    return null;
  } else {
    return null;
  }
}

var dirs = [{
    "name": "Test",
    "folders": [{
      "name": "Test2",
      "folders": [{
          "name": "test 3",
          "folders": []
        },
        {
          "name": "test 3",
          "folders": []
        }
      ]
    }]
  },
  {
    "name": "Main",
    "folders": [{
      "name": "Test2",
      "folders": [{
          "name": "test 3",
          "folders": []
        },
        {
          "name": "test 3",
          "folders": []
        }
      ]
    }]
  }
];
const testcase = "Test,Test2,test 3";
console.log('Searching for', testcase, ' in ', dirs)
console.log(findDir(testcase, dirs))

答案 2 :(得分:1)

这是2行解决方案:

您可以使用.find方法在某个级别上搜索文件夹:

(folder, name) => folder.folders.find(e => e.name == name);

然后,我编写了一个递归函数,它将为提供的路径的每个元素查找文件夹。它使用.reduce来跟踪父文件夹(acc)。在循环末尾,它返回最后一个文件夹数组(路径末尾的那个)。

path.split(',').reduce((acc, curr) => find(acc, curr), {folders:folders});

这是一个快速演示:

const find = (folder, name) => folder.folders.find(e => e.name == name);

const getFolders = path => path.split(',').reduce((acc, curr) => find(acc, curr), {folders:folders});

console.log(
  getFolders('home,New Folder,53w5r,test')
);
<script>
const folders = [{
  "name": "home",
  "folders": [{
    "name": "New Folder",
    "folders": [{
      "name": "53w5r",
      "folders": [{
        "name": "test",
        "folders": []
      }]
    }]
  }, {
    "name": "public folder",
    "folders": [{
      "name": "cold",
      "folders": []
    }, {
      "name": "hot",
      "folders": []
    }]
  }, {
    "name": "My Folder",
    "folders": []
  }]
}]
</script>