Objects.filter()
Syntax
function filter<T extends object>(object: T, predicate: (value: T[typeof key], key: keyof T) => boolean): Partial<T>
Creates a new object from an existing one with only the keys and values that match a condition.
Parameters | |
object
|
The object to filter. |
predicate
|
A predicate function that is called once for each enumerable key in the object and receives the value behind the key and the key itself as arguments. If this function returns |
Example
import { Objects } from 'potence';
const colors = {
red: 0xff0000,
yellow: 0xffff00,
green: 0x00ff00,
blue: 0x0000ff,
purple: 0xff00ff,
cyan: 0x00ffff
};
// bitwise and
Objects.filter(colors, color => (color & colors.blue) === colors.blue);
// returns:
// {
// blue: 0x0000ff,
// purple: 0xff00ff,
// cyan: 0x00ffff
// }
Remarks
This function is analogous to
Array.prototype.filter()
.
Note that this function does not perform any kind of copy on the key values.
If the key values are references, you may wish to use
Objects.map()
to clone them after the filter operation.