Skip to content

Instantly share code, notes, and snippets.

@rkhullar
Created August 25, 2021 13:24
Show Gist options
  • Save rkhullar/e4b73b96cc43a386f850a422fa2df995 to your computer and use it in GitHub Desktop.
Save rkhullar/e4b73b96cc43a386f850a422fa2df995 to your computer and use it in GitHub Desktop.
javascript-dictionary-comprehension
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