@kingjs/linq.where
v1.0.8
Published
Generates a sequence of elements composed of elements from another sequences which satisfy a specified condition.
Downloads
8
Readme
@kingjs/linq.where
Generates a sequence of elements composed of elements from another sequences which satisfy a specified condition.
Usage
Filter 0
, 1
, 2
, 3
to even numbers like this:
var where = require('@kingjs/linq.where');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');
var numbers = sequence(0, 1, 2, 3);
var isEven = function (x) { return x % 2 == 0; }
var evenNumbers = where.call(numbers, isEven);
toArray.call(evenNumbers);
result:
[0, 2]
Filter out every other element in 'a'
, 'b'
, 'c'
, 'd'
like this:
var where = require('@kingjs/linq.where');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');
var letters = sequence('a', 'b', 'c', 'd');
var isEvenIndex = function (x, i) { return i % 2 == 0; }
var everyOther = where.call(letters, isEvenIndex);
toArray.call(everyOther);
result:
['a', 'c']
API
declare function where(
this: Enumerable,
predicate: (x, i) => boolean,
): Enumerable
Interfaces
Enumerable
: See @kingjs/enumerable.define.
Parameters
this
: The sequence to filter.predicate
: The filtering predicate.x
: The value being considered for inclusion.i
: The index of the value being considered.
Return Value
A sequence filtered by the predicate.
Install
With npm installed, run
$ npm install @kingjs/linq.where
Acknowledgments
Like Enumerable.Where
.
License
MIT