You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
490 B

4 years ago
  1. # `mapToGroups()`
  2. The mapToGroups method iterates through the collection and passes each value to the given callback:
  3. ```js
  4. const collection = collect([
  5. { id: 1, name: 'A' },
  6. { id: 2, name: 'B' },
  7. { id: 3, name: 'C' },
  8. { id: 4, name: 'B' },
  9. ]);
  10. const groups = collection.mapToGroups((item, key) => [item.name, item.id]);
  11. // {
  12. // A: [1],
  13. // B: [2, 4],
  14. // C: [3],
  15. // }
  16. ```
  17. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/mapToGroups.js)