Created
August 25, 2021 13:24
-
-
Save rkhullar/e4b73b96cc43a386f850a422fa2df995 to your computer and use it in GitHub Desktop.
javascript-dictionary-comprehension
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 pipe(data, fn) { | |
if (typeof data === 'object' && data != null) { | |
return Object.fromEntries( | |
Object.keys(data).map(key => [key, fn(key, data[key])]) | |
); | |
} | |
} | |
const example = { | |
hello: 1, | |
world: 2 | |
} | |
const result = pipe(example, (key, val) => val * 2); | |
console.log(example); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment