返回介绍

groupBy

发布于 2019-05-31 13:12:56 字数 2138 浏览 879 评论 0 收藏 0

Creates a new ObjectLikeSequence comprising the elements in this one, grouped together according to some key. The value associated with each key in the resulting object-like sequence is an array containing all of the elements in this sequence with that key.

Signature

Sequence.groupBy = function(keyFn, valFn) { /*...*/ }
Sequence.groupBy = function groupBy(keyFn, valFn) {
  return new GroupedSequence(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 group them, or a string representing a parameter 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 for each instance to appear in the group, or a string representing a parameter to read from all the elements in this sequence.

returnsObjectLikeSequence

The new sequence.

Examples

function oddOrEven(x) {
  return x % 2 === 0 ? 'even' : 'odd';
}
function square(x) {
  return x*x;
}

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

Lazy(numbers).groupBy(oddOrEven)                     // sequence: { odd: [1, 3, 5], even: [2, 4] }
Lazy(numbers).groupBy(oddOrEven).get("odd")          // => [1, 3, 5]
Lazy(numbers).groupBy(oddOrEven).get("foo")          // => undefined
Lazy(numbers).groupBy(oddOrEven, square).get("even") // => [4, 16]

Lazy([
  { name: 'toString' },
  { name: 'toString' }
]).groupBy('name');
// => sequence: {
  'toString': [
{ name: 'toString' },
{ name: 'toString' }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文