Created
October 8, 2014 14:05
-
-
Save nekman/0da5e63bc19444ee323f to your computer and use it in GitHub Desktop.
This file contains 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 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