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

map

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

Creates a new sequence whose values are calculated by passing this sequence's elements through some mapping function.

Signature

Sequence.map = function(mapFn) { /*...*/ }
Sequence.map = function map(mapFn) {
  return new MappedSequence(this, createCallback(mapFn));
}
NameType(s)Description
mapFnFunction

The mapping function used to project this sequence's elements onto a new sequence. This function takes up to two arguments: the element, and the current index.

returnsSequence

The new sequence.

Examples

function addIndexToValue(e, i) { return e + i; }

Lazy([]).map(increment)              // sequence: []
Lazy([1, 2, 3]).map(increment)       // sequence: [2, 3, 4]
Lazy([1, 2, 3]).map(addIndexToValue) // sequence: [1, 3, 5]

Benchmarks

function increment(x) { return x + 1; }

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

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