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:
- Let M be the this value.
- If Type(M) is not Object, then throw a TypeError exception.
- If M does not have a [[MapData]] internal slot throw a TypeError exception.
- If M’s [[MapData]] internal slot is undefined, then throw a TypeError exception.
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If thisArg was supplied, let T be thisArg; else let T be undefined.
- Let entries be the List that is the value of M’s [[MapData]] internal slot.
- Let resultMap be the a new map object (as if by new Map()).
- 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.
- Return resultMap.
The length property of the filter method is 1.