Skip to content

Instantly share code, notes, and snippets.

@nekman
Created October 8, 2014 14:05
Show Gist options
  • Save nekman/0da5e63bc19444ee323f to your computer and use it in GitHub Desktop.
Save nekman/0da5e63bc19444ee323f to your computer and use it in GitHub Desktop.
function keys(object, deep) {
'use strict';
var keys = [];
function isObject(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
function resolveKeys(obj, result) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (deep && isObject(obj[key])) {
return resolveKeys(obj[key], result);
}
result.push(key);
}
}
return result;
}
return resolveKeys(object, keys);
}
keys(JSON.parse('{ "a": 1, "b": 2, "c": { "d": 1 } }'), true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment