a guest Jun 18th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fruits = ["Apple", "Banana", "Apple", "Durian", "Durian", "Durian"],
  2.     obj = {};
  3. fruits.reduce(function(prev, curr, index, arr){
  4.    obj[curr] ? obj[curr]++ : obj[curr] = 1;
  5. });
  6. console.log(obj); // {Banana: 1, Apple: 1, Durian: 3}
  8. fruit  carry (before operation)      carry (after operation, returned value)
  9. 1st iteration: Apple  {}                            {Apple:1}
  10. 2nd iteration: Banana {Apple:1}                     {Apple:1, Banana:1}
  11. 3rd iteration: Apple  {Apple:1, Banana:1}           {Apple:2, Banana:1}
  12. 4th iteration: Durian {Apple:2, Banana:1}           {Apple:2, Banana:1, Durian:1}
  13. 5th iteration: Durian {Apple:2, Banana:1, Durian:1} {Apple:2, Banana:1, Durian:2}
  14. 6th iteration: Durian {Apple:2, Banana:1, Durian:2} {Apple:2, Banana:1, Durian:3}
  16. arr.reduce(callback[, initialValue])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand