Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Created March 7, 2018 22:17
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save stephanbogner/4b590f992ead470658a5ebf09167b03d to your computer and use it in GitHub Desktop.
Save stephanbogner/4b590f992ead470658a5ebf09167b03d to your computer and use it in GitHub Desktop.
Create tree structure from paths array
var paths = [
["Account"],
["Account", "Payment Methods"],
["Account", "Payment Methods", "Credit Card"],
["Account", "Payment Methods", "Paypal"],
["Account", "Emails"],
["Account", "Emails", "Main Email"],
["Account", "Emails", "Backup Email"],
["Account", "Devices"],
["Account", "Devices", "Google Pixel"],
["Account", "Devices", "iPad Mini"],
["Account", "Devices", "Laptop"]
];
var tree = arrangeIntoTree(paths);
console.log(JSON.stringify(tree, null, 4));
// Result
// [
// {
// "name": "Account",
// "children": [
// {
// "name": "Payment Methods",
// "children": [
// {
// "name": "Credit Card",
// "children": []
// },
// {
// "name": "Paypal",
// "children": []
// }
// ]
// },
// {
// "name": "Emails",
// "children": [
// {
// "name": "Main Email",
// "children": []
// },
// {
// "name": "Backup Email",
// "children": []
// }
// ]
// },
// {
// "name": "Devices",
// "children": [
// {
// "name": "Google Pixel",
// "children": []
// },
// {
// "name": "iPad Mini",
// "children": []
// },
// {
// "name": "Laptop",
// "children": []
// }
// ]
// }
// ]
// }
// ]
function arrangeIntoTree(paths) {
// Adapted from http://brandonclapp.com/arranging-an-array-of-flat-paths-into-a-json-tree-like-structure/
var tree = [];
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var currentLevel = tree;
for (var j = 0; j < path.length; j++) {
var part = path[j];
var existingPath = findWhere(currentLevel, 'name', part);
if (existingPath) {
currentLevel = existingPath.children;
} else {
var newPart = {
name: part,
children: [],
}
currentLevel.push(newPart);
currentLevel = newPart.children;
}
}
}
return tree;
function findWhere(array, key, value) {
// Adapted from https://stackoverflow.com/questions/32932994/findwhere-from-underscorejs-to-jquery
t = 0; // t is used as a counter
while (t < array.length && array[t][key] !== value) { t++; }; // find the index where the id is the as the aValue
if (t < array.length) {
return array[t]
} else {
return false;
}
}
}
@ladz
Copy link

ladz commented Feb 21, 2019

Just what I needed 👍 Thanks!

@jacek-korzemski
Copy link

Hey, it's pure gold - exactly what I was Looking for. I am using it to build tree structure based on array of URL's. Of course, firstly I've split every url into an array of paths. Then to achieve fullUrl of current path I had to change one thing, link so:
var newPart = { fullUrl: path .slice(0, j + 1) .join("/") .replace("http/", "http://") .replace("https/", "https://"), name: part, children: [], };

@Cihatata
Copy link

Cihatata commented Sep 8, 2020

Just what I need Thanks

@leoliaolei
Copy link

Thanks.

@hanshou101
Copy link

Thanks , it works for me .

@msalekmouad
Copy link

Thanks a lot man saved my day

Copy link

ghost commented Dec 20, 2021

Thank u bro <3

@kamiyargit
Copy link

still perfect work 👍

@nickgrato
Copy link

might be missing something but does line 90 need to be there? seems to be working without it?

@stephanbogner
Copy link
Author

stephanbogner commented Feb 7, 2023

@nickgrato

might be missing something but does line 90 need to be there? seems to be working without it?

Honestly ... I don't know ... it's been so many years since I wrote the code ^^

@nickgrato
Copy link

@nickgrato

might be missing something but does line 90 need to be there? seems to be working without it?

Honestly ... I don't know ... it's been so many years since I wrote the code ^^

haha yes 5 years, very fair - this was helpful non the less thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment