Created
September 15, 2023 12:24
-
-
Save plugn/4c8cbb98bc640bbc03c98a2aa5795695 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 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