Skip to content

Instantly share code, notes, and snippets.

@bendc
Created October 26, 2016 08:00
Show Gist options
  • Save bendc/cf8d8ec959317fd8cca17636851e628e to your computer and use it in GitHub Desktop.
Save bendc/cf8d8ec959317fd8cca17636851e628e to your computer and use it in GitHub Desktop.
Object mapping
const odd = { a: 1, b: 3 };
const even = Object.entries(odd).reduce((object, pair) => {
const [key, value] = pair;
object[key] = value + 1;
return object;
}, {});
console.log(even); // => { a: 2, b: 4 }
@prathyvsh
Copy link

prathyvsh commented Oct 31, 2016

A suggestive refactor:

const odd = { a: 1, b: 3};

const kvmap = (f,o) => Object.entries(o).reduce((object, [k,v]) => {
object[k] = f(v);
return object;
}, {});

const even = kvmap(x => x+1, odd);

console.log(even); // => {a: 2, b: 4}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment