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.

37 lines
733 B

4 years ago
  1. # `countBy()`
  2. The countBy method counts the occurences of values in the collection. By default, the method counts the occurrences of every element:
  3. ```js
  4. const collection = collect([1, 2, 2, 2, 3]);
  5. const counted = collection.countBy();
  6. counted.all();
  7. // {
  8. // 1: 1,
  9. // 2: 3,
  10. // 3: 1,
  11. // }
  12. ```
  13. However, you pass a callback to the countBy method to count all items by a custom value:
  14. ```js
  15. const collection = collect([
  16. 'alice@gmail.com',
  17. 'bob@yahoo.com',
  18. 'carlos@gmail.com',
  19. ]);
  20. const counted = collection.countBy(email => email.split('@')[1]);
  21. counted.all();
  22. // {
  23. // 'gmail.com': 2,
  24. // 'yahoo.com': 1,
  25. // }
  26. ```
  27. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/countBy.js)