Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 28, 2015 08:29
Show Gist options
  • Save creationix/7471477 to your computer and use it in GitHub Desktop.
Save creationix/7471477 to your computer and use it in GitHub Desktop.
function getKeys(obj) {
all = {};
function get(obj) {
var keys = Object.keys(obj);
for (var i = 0, l= keys.length; i < l; i++) {
var key = keys[i];
all[key] = true;
var value = obj[key];
if (value instanceof Object) get(value);
}
}
get(obj);
return Object.keys(all);
}
@creationix
Copy link
Author

Keep in mind, this needs an implementation of Object.keys which can be polyfilled with:

Object.keys = function (obj) {
  var keys = [];
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) keys.push(key);
  }
  return keys;
};

Also there is no protection against cycles in the object. That could added at a performance and memory use cost if desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment