Skip to content

Instantly share code, notes, and snippets.

@iskugor
Created November 14, 2013 18:20
Show Gist options
  • Save iskugor/7471711 to your computer and use it in GitHub Desktop.
Save iskugor/7471711 to your computer and use it in GitHub Desktop.
Get unique keys in nested object
function getKeys(obj) {
var keys = Object.keys(obj);
keys.forEach(function(val) {
if (obj[val] instanceof Object) {
keys = keys.concat(getKeys(obj[val]));
}
});
return keys;
}
function getUniqueKeys(obj) {
var keys = getKeys(obj);
keys = keys.filter(function (e, i, keys) {
return keys.lastIndexOf(e) === i;
});
return keys;
}
alert(getUniqueKeys({
key1: 'one',
key2: {
key1: {
another: 42
}
}
})); //produces "key2,key1,another"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment