absjabed

/** ES6 Modern JavaScript Methods **/

Jan 2nd, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ES6 Modern JavaScript Methods **/
  2.  
  3. const companies= [
  4.   {name: "Company One", category: "Finance", start: 1981, end: 2004},
  5.   {name: "Company Two", category: "Retail", start: 1992, end: 2008},
  6.   {name: "Company Three", category: "Auto", start: 1999, end: 2007},
  7.   {name: "Company Four", category: "Retail", start: 1989, end: 2010},
  8.   {name: "Company Five", category: "Technology", start: 2009, end: 2014},
  9.   {name: "Company Six", category: "Finance", start: 1987, end: 2010},
  10.   {name: "Company Seven", category: "Auto", start: 1986, end: 1996},
  11.   {name: "Company Eight", category: "Technology", start: 2011, end: 2016},
  12.   {name: "Company Nine", category: "Retail", start: 1981, end: 1989}
  13. ];
  14.  
  15. const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
  16.  
  17. // for(let i = 0; i < companies.length; i++) {
  18. //   console.log(companies[i]);
  19. // }
  20.  
  21. // forEach
  22.  
  23. // companies.forEach(function(company) {
  24. //   console.log(company.name);
  25. // });
  26.  
  27. // filter
  28.  
  29. // Get 21 and older
  30.  
  31. // let canDrink = [];
  32. // for(let i = 0; i < ages.length; i++) {
  33. //   if(ages[i] >= 21) {
  34. //     canDrink.push(ages[i]);
  35. //   }
  36. // }
  37.  
  38. // const canDrink = ages.filter(function(age) {
  39. //   if(age >= 21) {
  40. //     return true;
  41. //   }
  42. // });
  43.  
  44. const canDrink = ages.filter(age => age >= 21);
  45.  
  46. // Filter retail companies
  47.  
  48. // const retailCompanies = companies.filter(function(company) {
  49. //   if(company.category === 'Retail') {
  50. //     return true;
  51. //   }
  52. // });
  53.  
  54. const retailCompanies = companies.filter(company => company.category === 'Retail');
  55.  
  56. // Get 80s companies
  57.  
  58. const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
  59.  
  60. // Get companies that lasted 10 years or more
  61.  
  62. const lastedTenYears = companies.filter(company => (company.end - company.start >= 10));
  63.  
  64. // map
  65.  
  66. // Create array of company names
  67. // const companyNames = companies.map(function(company) {
  68. //   return company.name;
  69. // });
  70.  
  71. // const testMap = companies.map(function(company) {
  72. //   return `${company.name} [${company.start} - ${company.end}]`;
  73. // });
  74.  
  75. // const testMap = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
  76.  
  77. // const ageMap = ages
  78. //   .map(age => Math.sqrt(age))
  79. //   .map(age => age * 2);
  80.  
  81.  
  82.  
  83. // sort
  84.  
  85. // Sort companies by start year
  86.  
  87. // const sortedCompanies  = companies.sort(function(c1, c2) {
  88. //   if(c1.start > c2.start) {
  89. //     return 1;
  90. //   } else {
  91. //     return -1;
  92. //   }
  93. // });
  94.  
  95. // const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
  96.  
  97. // Sort ages
  98. // const sortAges = ages.sort((a, b) => a - b);
  99.  
  100. // console.log(sortAges);
  101.  
  102.  
  103. // reduce
  104.  
  105. // let ageSum = 0;
  106. // for(let i = 0; i < ages.length; i++) {
  107. //   ageSum += ages[i];
  108. // }
  109.  
  110. // const ageSum = ages.reduce(function(total, age) {
  111. //   return total + age;
  112. // }, 0);
  113.  
  114. // const ageSum = ages.reduce((total, age) => total + age, 0);
  115.  
  116. // Get total years for all companies
  117.  
  118. // const totalYears = companies.reduce(function(total, company) {
  119. //   return total + (company.end - company.start);
  120. // }, 0);
  121.  
  122. const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
  123.  
  124. // Combine Methods
  125.  
  126. const combined = ages
  127.   .map(age => age * 2)
  128.   .filter(age => age >= 40)
  129.   .sort((a, b) => a - b)
  130.   .reduce((a, b) => a + b, 0);
  131.  
  132. console.log(combined);
Advertisement
Add Comment
Please, Sign In to add comment