Last active
May 28, 2018 13:55
-
-
Save zekrom-vale/b6aeded8af51d7e60dbbbdbc8646f0ed to your computer and use it in GitHub Desktop.
Flatten an object or get the deep keys of one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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