Skip to content

Instantly share code, notes, and snippets.

@azasypkin
Last active March 17, 2017 14:08
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 azasypkin/7c5a0e3065a25db101236544ed06d608 to your computer and use it in GitHub Desktop.
Save azasypkin/7c5a0e3065a25db101236544ed06d608 to your computer and use it in GitHub Desktop.
Javascript MapReduce example
const map = new Map([[1, true], [2, true], [3, false], [4, false], [5, true]]);
const processedMap = Array.from(map.entries())
.map(([key, value]) => [value, key])
.reduce((acc, [key, value]) => acc.set(key, value + (acc.get(key) || 0)), new Map());
console.log('---------- Original Map ----------');
for (const [key, value] of map.entries()) {
console.log(`key: ${key}, value: ${value}`);
}
console.log('---------- Processed Map ----------');
for (const [key, value] of processedMap.entries()) {
console.log(`key: ${key}, value: ${value}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment