当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

filter

优质
小牛编辑
133浏览
2023-12-01

Creates a new sequence whose values are the elements of this sequence which satisfy the specified predicate.

Signature

Sequence.filter = function(filterFn) { /*...*/ }
Sequence.filter = function filter(filterFn) {
  return new FilteredSequence(this, createCallback(filterFn));
}
NameType(s)Description
filterFnFunction

The predicate to call on each element in this sequence, which returns true if the element should be included.

returnsSequence

The new sequence.

Examples

var numbers = [1, 2, 3, 4, 5, 6];

Lazy(numbers).filter(isEven) // sequence: [2, 4, 6]

Benchmarks

function isEven(x) { return x % 2 === 0; }

var smArr = Lazy.range(10).toArray(),
lgArr = Lazy.range(100).toArray();

Lazy(smArr).filter(isEven).each(Lazy.noop) // lazy - 10 elements
Lazy(lgArr).filter(isEven).each(Lazy.noop) // lazy - 100 elements
_.each(_.filter(smArr, isEven), _.noop)    // lodash - 10 elements
_.each(_.filter(lgArr, isEven), _.noop)    // lodash - 100 elements
Implementation10 elements100 elements
lazy
lodash