Skip to content

Instantly share code, notes, and snippets.

@vjeux
Last active November 15, 2016 19:00
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 vjeux/8fff638d092bc12d3eb54789668d1172 to your computer and use it in GitHub Desktop.
Save vjeux/8fff638d092bc12d3eb54789668d1172 to your computer and use it in GitHub Desktop.

Specification:

Map.prototype.map = function(cb, context) { return new Map(Array.from(this).map(cb, context)); }
Map.prototype.filter = function(cb, context) { return new Map(Array.from(this).filter(cb, context)); }

Usage example:

new Map([['keya', 'valuea'], ['keyb', 'valueb']])
  .map(([key, value]) => [key + 1, value + 1])
  .filter(([key, value]) => key === 'keya1')
// Map {"keya1" => "valuea1"}
@ljharb
Copy link

ljharb commented Nov 15, 2016

Array.from takes a second mapper argument.

@phpnode
Copy link

phpnode commented Nov 15, 2016

@ljharb but it can't be bound to a context... right?

@xgrommx
Copy link

xgrommx commented Nov 15, 2016

Map.prototype.map = function(cb, context) { return new Map(Array.from(this, cb, context)); }
Map.prototype.filter = function(cb, context) { return new Map(Array.from(this, context).filter(cb)); }
new Map([...m].map(cb));
new Map([...m].filter(cb));

@xgrommx
Copy link

xgrommx commented Nov 15, 2016

Map.prototype.filter = function(cb, context) { return new Map(Array.from(this, identity, context).filter(cb)); }

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