Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. const animals = [
  2. {
  3. "name": "cat",
  4. "size": "small",
  5. "weight": 5
  6. },
  7. {
  8. "name": "dog",
  9. "size": "small",
  10. "weight": 10
  11. },
  12. {
  13. "name": "lion",
  14. "size": "medium",
  15. "weight": 150
  16. },
  17. {
  18. "name": "elephant",
  19. "size": "big",
  20. "weight": 5000
  21. }
  22. ];
  23.  
  24. // Map - do same operation on each array element, return an array of same size
  25. animals.map(animal => animal.name);
  26. // ["cat", "dog", "lion", "elephant"]
  27.  
  28. // Filter - return array that matching certain criteria
  29. animals.filter(animal => animal.size === "small");
  30. /*
  31. [
  32. {
  33. "name": "cat",
  34. "size": "small",
  35. "weight": 5
  36. },
  37. {
  38. "name": "dog",
  39. "size": "small",
  40. "weight": 10
  41. }
  42. ]
  43. */
  44.  
  45. // Reduce - use the values in an array and return something completely new
  46. animals.reduce((weight, animal) => {return weight += animal.weight}, 0);
  47. // 5165
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement