Created
April 14, 2019 14:43
-
-
Save westc/b66a8bf46a00295f2624b7161da71c79 to your computer and use it in GitHub Desktop.
Get path-value pairs for an object. Doesn't support recursive objects.
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 getPathEntries(obj) { | |
let result = []; | |
let arr = Object.keys(obj).map(key => ({ parent: obj, path: key, key })); | |
for (let i = 0; i < arr.length; i++) { | |
let { parent, path, key } = arr[i]; | |
let value = parent[key]; | |
result.push([path, value]); | |
if (!(value instanceof RegExp) && 'object' === typeof value) { | |
arr.push.apply(arr, Object.keys(value).map(key => ({ parent: value, path: path + `.${key}`, key }))); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment