Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created October 1, 2014 22:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitrySoshnikov/82b46f4674acee5cedc2 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/82b46f4674acee5cedc2 to your computer and use it in GitHub Desktop.
Map.prototype.filter

Map.prototype.filter ( callbackfn [ , thisArg ] )

NOTE callbackfn should be a function that accepts three arguments. filter calls callbackfn once for each key/value pair present in the map object, in key insertion order, and returns a new filtered map. callbackfn is called only for keys of the map which actually exist; it is not called for keys that have been deleted from the map.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the item, the key of the item, and the Map object being traversed.

filter does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

When the filter method is called with one or two arguments, the following steps are taken:

  1. Let M be the this value.
  2. If Type(M) is not Object, then throw a TypeError exception.
  3. If M does not have a [[MapData]] internal slot throw a TypeError exception.
  4. If M’s [[MapData]] internal slot is undefined, then throw a TypeError exception.
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let entries be the List that is the value of M’s [[MapData]] internal slot.
  8. Let resultMap be the a new map object (as if by new Map()).
  9. Repeat for each Record {[[key]], [[value]]} e that is an element of entries, in original key insertion order
  • If e.[[key]] is not empty, then
    • Let selected be the result of calling the [[Call]] internal method of callbackfn with T as thisArgument and a List containing e.[[value]], e.[[key]], and M as argumentsList.
    • ReturnIfAbrupt(selected).
    • If ToBoolean(selected) is true then
      • Call resultMap's set explicit method passing e.[[key]] as key, and e.[[value]] as value.
  1. Return resultMap.

The length property of the filter method is 1.

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