Skip to content

Instantly share code, notes, and snippets.

@rldotai
Created January 31, 2016 06:21
Show Gist options
  • Save rldotai/645cc13ce98bc834cb9b to your computer and use it in GitHub Desktop.
Save rldotai/645cc13ce98bc834cb9b to your computer and use it in GitHub Desktop.
Flatten a tree into an array by combining the keys.
// Flatten a tree into an array by combining the keys.
var flatten_tree = function(obj) {
var sep = '/';
var ret = [];
function _flat(elem, base) {
base = ((base === undefined) ? '' : base + sep);
for (let i in elem){
if (!elem.hasOwnProperty(i)) continue;
let val = elem[i];
if ((typeof val) === 'object') {
if (_.isEmpty(val)) {
ret.push(base + i);
} else {
_flat(elem[i], base + i);
}
}
}
return ret;
}
return _flat(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment