Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // Map.
  2. // Allows us to loop an array to modify or add props in some way.
  3. // arr.map((value, index, array)) - map recieves a callback as an argument which is given each current
  4. // value, index and array of the iteration.
  5.  
  6. const songs = [
  7. { id: 1, name: "best song", artist: "randoms" },
  8. { id: 2, name: "another song", artist: "dfgg" },
  9. { id: 3, name: "boring song", artist: "sdfs" },
  10. { id: 4, name: "best song", artist: "gdfg" },
  11. ];
  12.  
  13.  
  14. const map = songs.map((song, index, array) => {
  15. // Maps loop through arrays.
  16. const { artist, ...rest } = song;
  17. // console.log('Artist: ', artist);
  18. // console.log('Rest of properties: ', rest);
  19.  
  20. return {
  21. ...rest,
  22. anotherProp: "adding to the new obj"
  23. };
  24.  
  25. })
  26.  
  27. //console.log(map);
  28.  
  29.  
  30. // Filter.
  31. // Works like a map, except the callback func needs to return either true or false.
  32. // If it returns true then the array keeps that element and if it returns false the element is filtered out.
  33.  
  34. const array = [1,2,3,4,5];
  35.  
  36. const onlyEvenNums = array.filter((value, index, array) => {
  37. // Filter will loop through each value in array and add to a new array in condition matches.
  38. return value % 2 === 0;
  39. })
  40.  
  41. // console.log(onlyEvenNums);
  42.  
  43.  
  44. let strings = ["hello", "there", "you", "hero"];
  45. let filterStrings = strings.filter(string => {
  46. // Loop though each properties and if condition matches, add to new array.
  47. return string.includes("er");
  48. });
  49.  
  50. // console.log(filterStrings);
  51.  
  52. let filterArrayOfObjects = songs.filter(song => {
  53. return song.name === "best song";
  54. });
  55.  
  56. // console.log(filterArrayOfObjects);
  57.  
  58.  
  59. // Reduce.
  60. // Reduce takes an array and reduces it into a single value. For example, with an array of numbers you can easily find the average of all values
  61. // the callback differs though to map and filter in the sense it now recieves the accumulator (i.e. it adds up all the return values).
  62. // array.reduce((acc, currValue, currindex, array))
  63. let nums = [1,2,3,4];
  64.  
  65. var max = nums.reduce(function(a, b) {
  66. return Math.max(a, b);
  67. });
  68.  
  69. // Iterate through array and add each properties (currentValue) to the accumulator of previous value.
  70. const sum = nums.reduce((accumulator, currentValue, currentIndex, array) => {
  71. return accumulator + currentValue;
  72. });
  73.  
  74. //console.log(sum);
  75.  
  76. let obj = songs.reduce((accumulator, currentValue) => {
  77. const { artist } = currentValue;
  78.  
  79. // Accumulator
  80. accumulator[artist] = artist;
  81.  
  82. return {...accumulator};
  83. }, {});
  84.  
  85. console.log(obj);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement