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
demo.js
RFID TEST
Range List for JavaScript
Rest Params
JS - check 2 arrays, add 1 or 2, logical operations
ddd
A01 Implement Queue with Limited Size of Arrays
Accept matching keys in object
Counting digits
example for node promises