Skip to content

Instantly share code, notes, and snippets.

@ardeearam
Created August 22, 2019 06:27
Show Gist options
  • Save ardeearam/ae9eef0e0b23b210c2bfbf3b374e5b07 to your computer and use it in GitHub Desktop.
Save ardeearam/ae9eef0e0b23b210c2bfbf3b374e5b07 to your computer and use it in GitHub Desktop.
Map.map() should be a thing in ES6. It just makes sense.
/**
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