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

indexBy

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

Creates a new ObjectLikeSequence comprising the elements in this one, indexed according to some key.

Signature

Sequence.indexBy = function(keyFn, valFn) { /*...*/ }
Sequence.indexBy = function(keyFn, valFn) {
  return new IndexedSequence(this, keyFn, valFn);
}
NameType(s)Description
keyFnFunction|string

The function to call on the elements in this sequence to obtain a key by which to index them, or a string representing a property to read from all the elements in this sequence.

valFnFunction|string

(Optional) The function to call on the elements in this sequence to assign to the value of the indexed object, or a string representing a parameter to read from all the elements in this sequence.

returnsSequence

The new sequence.

Examples

var people = [
  { name: 'Bob', age: 25 },
  { name: 'Fred', age: 34 }
];

var bob  = people[0],
fred = people[1];

Lazy(people).indexBy('name')        // sequence: { 'Bob': bob, 'Fred': fred }
Lazy(people).indexBy('name', 'age') // sequence: { 'Bob': 25, 'Fred': 34 }