Skip to content

Instantly share code, notes, and snippets.

@zekrom-vale
Last active May 28, 2018 13:55
Show Gist options
  • Save zekrom-vale/b6aeded8af51d7e60dbbbdbc8646f0ed to your computer and use it in GitHub Desktop.
Save zekrom-vale/b6aeded8af51d7e60dbbbdbc8646f0ed to your computer and use it in GitHub Desktop.
Flatten an object or get the deep keys of one
function getDeepKeys(obj,key,d="/"){
if(typeof(obj)!=="object")return null;
var arr=[];
for(let i in obj){
if(typeof(obj[i])==="object")arr=arr.concat(getDeepKeys(obj[i],key?key+d+i:i));
else arr.push(key?key+d+i:i);
}
return arr;
}
function objectFlatten(obj,key,d="/"){
if(typeof(obj)!=="object")return null;
var arr={};
for(let i in obj){
if(typeof(obj[i])==="object"){
let o=objectFlatten(obj[i],key?key+d+i:i);
for(let n in o) arr[n]=o[n];
}
else arr[key?key+d+i:i]=obj[i];
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment