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.

36 lines
622 B

4 years ago
  1. # `each()`
  2. The each method iterates over the items in the collection and passes each item to a callback:
  3. ```js
  4. let sum = 0;
  5. const collection = collect([1, 3, 3, 7]);
  6. collection.each((item) => {
  7. sum += item;
  8. });
  9. // console.log(sum);
  10. // 14
  11. ```
  12. If you would like to stop iterating through the items, you may return false from your callback:
  13. ```js
  14. let sum = 0;
  15. const collection = collect([1, 3, 3, 7]);
  16. collection.each((item) => {
  17. sum += item;
  18. if (sum > 5) {
  19. return false;
  20. }
  21. });
  22. // console.log(sum);
  23. // 7
  24. ```
  25. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/each.js)