Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // Reduces an array or object to a single value by repetitively calling
  2. // iterator(accumulator, item) for each item. accumulator should be
  3. // the return value of the previous iterator call.
  4. //
  5. // You can pass in a starting value for the accumulator as the third argument
  6. // to reduce. If no starting value is passed, the first element is used as
  7. // the accumulator, and is never passed to the iterator. In other words, in
  8. // the case where a starting value is not passed, the iterator is not invoked
  9. // until the second element, with the first element as its second argument.
  10. //
  11. // Example:
  12. // var numbers = [1,2,3];
  13. // var sum = _.reduce(numbers, function(total, number){
  14. // return total + number;
  15. // }, 0); // should be 6
  16. //
  17. // var identity = _.reduce([5], function(total, number){
  18. // return total + number * number;
  19. // }); // should be 5, regardless of the iterator function passed in
  20. // No accumulator is given so the first element is used.
  21.  
  22.  
  23. _.reduce = function(collection, iterator, accumulator) {
  24.  
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement