I need some advice concerning string manipulation. This is a possible string:
"/branches/projects/MyTool/MyTool/someOtherFolderOrFile"
What I need is an array with the following in it:
['/branches', '/branches/projects','/branches/projects/MyTool','/branches/projects/MyTool/MyTool']
So to clarify what this array should contain: All parent paths to the last file or folder. The last file or folder is excluded in the array.
I know I can split the string at "/" and then could iterate over the array, concatenate strings and delete the last entry from the array. This method I would call recursively until there is no element in the array left. The concatenated string would then be saved at every iteration to a new array which would hold the desired result at the end......
However I asked me how to do it very clean and simple and I am sure someone of you can come up with a superb solution :)
EDIT Here is my current solution I came up with just now:
var string = "/branches/projects/MyTool/MyTool/anotherFolderOrFile";
var helperArr = string.split("/");
var endArray = [];
getParentArray();
console.log(endArray);
function getParentArray() {
if(helperArr.length == 1) {
return;
}
else {
var helperString = "";
for(var i = 0;i<helperArr.length;i++) {
helperString = helperString+helperArr[i];
if(i!= helperArr.length -1) {
helperString = helperString+"/";
}
}
endArray.push(helperString);
helperArr.length = helperArr.length-1;
getParentArray();
}
}