_.groupBy
Group items by key.
- // Underscore/Lodash
- var grouped = _.groupBy(['one', 'two', 'three'], 'length')
- console.log(grouped)
- // output: {3: ["one", "two"], 5: ["three"]}
- // Native
- var grouped = ['one', 'two', 'three'].reduce((r, v, i, a, k = v.length) => ((r[k] || (r[k] = [])).push(v), r), {})
- console.log(grouped)
- // output: {3: ["one", "two"], 5: ["three"]}
- // Underscore/Lodash
- var grouped = _.groupBy([1.3, 2.1, 2.4], num => Math.floor(num))
- console.log(grouped)
- // output: {1: [1.3], 2: [2.1, 2.4]}
- // Native
- var grouped = [1.3, 2.1, 2.4].reduce((r, v, i, a, k = Math.floor(v)) => ((r[k] || (r[k] = [])).push(v), r), {})
- console.log(grouped)
- // output: {1: [1.3], 2: [2.1, 2.4]}
Browser Support for Array.prototype.reduce()
✔ | ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .