Created
August 22, 2019 06:27
-
-
Save ardeearam/ae9eef0e0b23b210c2bfbf3b374e5b07 to your computer and use it in GitHub Desktop.
Map.map() should be a thing in ES6. It just makes sense.
This file contains hidden or 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
/** | |
How to use: | |
let m = new Map(); | |
m.set(1, 1); | |
m.set(2, 2); | |
// Map(2) {1 => 1, 2 => 2} | |
let n = m.map((key, value) => value * 2); | |
// Map(2) {1 => 2, 2 => 4} | |
**/ | |
Map.prototype.map = function(mapfn) { | |
//mapfn((key, value) => ...) | |
let newMap = new Map(); | |
for(let key of this.keys()) { | |
let value = this.get(key); | |
newMap.set(key, mapfn(key, value)); | |
} | |
return newMap; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment