Skip to content Skip to sidebar Skip to footer

Get Directory Path In Array Of Objects From Directory Path As String

Been searching SO posts, nothing answers this for me yet. I have an Array of folders like such : [{ 'name': 'home', 'folders': [{ 'name': 'New Folder', 'folders': [{

Solution 1:

Assuming that your comma-separated path will always be valid, you can use this simple method for traversing the data:

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.foldersreturn index === folderNames.length-1 ? currFolder : currFolder.folders;
}, data);
console.log(result);

Solution 2:

Here is my implementation without reduce, it will return null if the directory wasn't found.

const dirSeperator = ",";

functionfindDir(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)
          returnfindDir(path.replace(n + dirSeperator, ''), dirs[i].folders)
        elseif (parts.length == 1)
          return dirs[i]
      }
    }

    returnnull;
  } else {
    returnnull;
  }
}

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

Solution 3:

Here's a 2 line solution:

You can use the .find method to search for a folder on a level:

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

Then I have written a recursive function that will look up the folders for each element of the provided path. It uses .reduce to keep track of the parent folder (acc). At the end of the loop it returns the last folder array (the one at the end of the path).

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

Here's a quick demo:

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

constgetFolders = 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>

Post a Comment for "Get Directory Path In Array Of Objects From Directory Path As String"