Skip to content

Instantly share code, notes, and snippets.

@plugn
Created September 15, 2023 12:24
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 plugn/4c8cbb98bc640bbc03c98a2aa5795695 to your computer and use it in GitHub Desktop.
Save plugn/4c8cbb98bc640bbc03c98a2aa5795695 to your computer and use it in GitHub Desktop.
function walkObjectRecursive(source, mapper = (v, k) => ({ [k]: v })) {
if (typeof source !== 'object' || source === null || source === undefined) {
return source
}
if (Array.isArray(source)) {
return source.reduce((acc, val, index) => [...acc, mapper(walkObjectRecursive(val, mapper), index)], [])
}
return Object.keys(source).reduce(
(acc, key) => ({
...acc,
...mapper(walkObjectRecursive(source[key], mapper), key),
}),
{},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment