Last active
March 17, 2017 14:08
-
-
Save azasypkin/7c5a0e3065a25db101236544ed06d608 to your computer and use it in GitHub Desktop.
Javascript MapReduce example
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
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