Skip to content

Instantly share code, notes, and snippets.

@westc
Created April 14, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/b66a8bf46a00295f2624b7161da71c79 to your computer and use it in GitHub Desktop.
Save westc/b66a8bf46a00295f2624b7161da71c79 to your computer and use it in GitHub Desktop.
Get path-value pairs for an object. Doesn't support recursive objects.
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