Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tree Structure
var arr = [ { name: 'test', size: 0, type: 'directory', path: '/storage/test' }, { name: 'asdf', size: 170, type: 'directory', path: '/cloud/test/asdf' }, { name: '2.txt', size: 0, type: 'file', path: '/storage/test/asdf/2.txt' } ]; var tree = {}; function addnode(obj){ var splitpath = obj.path.replace(/^\/|\/$/g, "").split('/'); var ptr = tree; for (i=0;i<splitpath.length;i++) { node = { name: splitpath[i], type: 'directory' }; if(i == splitpath.length-1) { node.size = obj.size; node.type = obj.type; } ptr[splitpath[i]] = (ptr[splitpath[i]]) ? ptr[splitpath[i]] : node; ptr[splitpath[i]].children = (ptr[splitpath[i]].children) ? ptr[splitpath[i]].children : {}; ptr = ptr[splitpath[i]].children; } } arr.map(addnode); console.log(require('util').inspect(tree, {depth:null}));
run
|
edit
|
history
|
help
0
Union of two sorted arrays
Find the missing number in array
ddd
localhost:8080/?name=2017&lname=July
Rest Params
find all prime factors of a number
Add property to object
horsevalue
Accept matching keys in object
apply