Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
  2.  
  3. // cities.forEach((value, index, array) => {
  4. // console.log(`${index + 1} ${value}`); //output: 1 ROME, 2 PARIS, 3 LONDON, 4 LOS ANGELES, 5 VIENNA
  5. // });
  6.  
  7. // //we can use it to invert the "cities" array...even though with reverse() would be better 😉
  8. // const invertedCities = [];
  9. // cities.forEach((value, index, array) => invertedCities.unshift(value));
  10. // console.log(invertedCities); //output: ["VIENNA", "LOS ANGELES", "LONDON", "PARIS", "ROME"]
  11.  
  12. //Let's create a new array with all our numbers squared
  13. // const numbers = [1, 2, 3, 4, 5];
  14.  
  15. // const squaredNumbers = numbers.map(number => number * number);
  16. // console.log(squaredNumbers); //output: [1, 4, 9, 16, 25]
  17.  
  18. // //We all know which is the most beautiful city in the World... 😉
  19. // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
  20. // const bestCity = cities.map(city => (city === 'ROME' ? city : 'ROME'));
  21. // console.log(bestCity); //output: ["ROME", "ROME", "ROME", "ROME", "ROME"]
  22.  
  23. // //Let's create an array of HTML tag
  24. // const html = cities.map(city => `<li>${city}</li>`);
  25. // console.log(html); //output: ["<li>ROME</li>", "<li>PARIS</li>", "<li>LONDON</li>", "<li>LOS ANGELES</li>", "<li>VIENNA</li>"]
  26.  
  27. // //Transform an array of strings in an array of objects
  28. // const metalBands = ['IRON MAIDEN', 'SLAYER', 'JUDAS PRIEST'];
  29. // const collection = metalBands.map((band, index) => {
  30. // let obj = {}; //create an empty object at any call of the loop
  31. // obj.id = index; //create a key called "id" and set it equal to our index parameter
  32. // obj.band = band; //create a key called "band" and set it equal to our band parameter
  33. // return obj; //return an object at any call with key/value pairs like this: {id: 'index', band: 'band-name'}
  34. // });
  35. // console.log(collection); //output: [{id: 0, band: "IRON MAIDEN"},{id: 1, band: "SLAYER"}, {id: 2, band: "JUDAS PRIEST"}]
  36.  
  37. // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  38.  
  39. // //Return an array of even values from numbers
  40. // const evens = numbers.filter(num => num % 2 === 0);
  41. // console.log(evens); //output: [2, 4, 6, 8, 10]
  42.  
  43. // //Return an array of odd values from numbers
  44. // const odds = numbers.filter(num => num % 2 !== 0);
  45. // console.log(odds); //output: [1, 3, 5, 7, 9]
  46.  
  47. // //All the roads lead to Rome 🚋 ...
  48. // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
  49. // const rome = cities.filter(city => city == 'ROME');
  50. // console.log(rome); //output: ["ROME"]
  51.  
  52. // //You can chain together this methods 🚀🚀🚀
  53. // //Example: let's square all even numbers
  54. // const squaredEvens = numbers.filter(num => num % 2 === 0).map(num => num * num);
  55. // console.log(squaredEvens); //output: [4, 16, 36, 64, 100];
  56.  
  57. // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  58. // //Let's start with a basic example
  59. // //Sum all the numbers in an array of integers
  60. // const sum = numbers.reduce((accumulator, currentValue, currentIndex, array) => {
  61. // //Look we set the Initial Value to 0, because it is a sum so the count starts at 0
  62. // //So our Accumulator is 0 at first call and we sum it with the Current Value that is 1 at first call...
  63. // //the new Accumulator will be 0 + 1 = 1 ...at any call the Current Value will be added to it
  64. // //till the end of the array
  65. // return accumulator + currentValue;
  66. // }, 0);
  67. // console.log(sum); // output: 55
  68.  
  69. // //Same example setting the Initial Value to 1 instead of 0 will return ... 56
  70. // const anotherSum = numbers.reduce((accumulator, currentValue, currentIndex, array) => {
  71. // return accumulator + currentValue;
  72. // }, 1);
  73. // console.log(anotherSum); // output: 56
  74.  
  75. // //Sum from an array of objects
  76. // const integers = [{ x: 1 }, { x: 2 }, { x: 3 }];
  77. // const anotherSumAgain = integers.reduce((acc, val, idx, array) => {
  78. // return acc + val.x;
  79. // }, 0);
  80. // console.log(anotherSumAgain); // output: 6
  81.  
  82. // //Count vowels in a string (even though it's easier with regex 😉)
  83. // const maryPoppins = 'supercalifragilisticexpialidocious';
  84. // const onlyVowels = maryPoppins.replace(/[^aeiou]/gi, ''); //'ueaiaiiieiaioiou'
  85. // const arrOfVowels = [...onlyVowels]; //["u", "e", "a", "i", "a", "i", "i", "i", "e", "i", "a", "i", "o", "i", "o", "u"]
  86. // const countVowels = arrOfVowels.reduceRight((acc, val) => {
  87. // acc.hasOwnProperty(val) ? (acc[val] += 1) : (acc[val] = 0);
  88. // return acc;
  89. // }, {});
  90. // console.log(countVowels); // output: {u: 1, e: 1, a: 2, i: 6, o: 1}
  91.  
  92. // //Flatten an array of arrays
  93. // //Hey! I know ES2019 gave us flat and flatMap methods, but we MUST learn reduce() now 😉
  94. // const someData = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
  95. // const flatten = someData.reduce((acc, val) => {
  96. // //set the initial value to an empty array
  97. // return acc.concat(val);
  98. // }, []);
  99. // console.log(flatten); // output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  100.  
  101. // //Sum all countries population except China
  102. // const population = [
  103. // {
  104. // country: 'China',
  105. // pop: 1409517397
  106. // },
  107. // {
  108. // country: 'India',
  109. // pop: 1339180127
  110. // },
  111. // {
  112. // country: 'USA',
  113. // pop: 324459463
  114. // },
  115. // {
  116. // country: 'Indonesia',
  117. // pop: 263991379
  118. // }
  119. // ];
  120. // const sumPopulationButNotChina = population.reduce((acc, val) => {
  121. // // we use the Ternary Operator as a "filter"
  122. // //if val.country is not equal to "China" we add the value to the accumulator
  123. // //if it is equal to "China" we add nothing and simply return the accumulator
  124. // return val.country !== 'China' ? acc + val.pop : acc;
  125. // }, 0);
  126.  
  127. // console.log(sumPopulationButNotChina); // output: 1927630969
  128.  
  129. //Check if all values are more than zero
  130. // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  131. // const moreThanZero = numbers.every((val, index, array) => val > 0);
  132. // console.log(moreThanZero); //true
  133.  
  134. // const numbersAgain = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  135. // const moreThanZeroAgain = numbersAgain.every((val, index, array) => val > 0);
  136. // console.log(moreThanZeroAgain); //false
  137.  
  138. // //Check if there are more than 1000000 people in all the countries
  139. // const population = [
  140. // {
  141. // country: 'China',
  142. // pop: 1409517397
  143. // },
  144. // {
  145. // country: 'India',
  146. // pop: 1339180127
  147. // },
  148. // {
  149. // country: 'USA',
  150. // pop: 324459463
  151. // },
  152. // {
  153. // country: 'Indonesia',
  154. // pop: 263991379
  155. // }
  156. // ];
  157.  
  158. // const check = population.every(val => val.pop > 1000000);
  159. // console.log(check); //true
  160.  
  161. //Check if a value is more than zero in the array
  162. // const numbers = [-1, -2, 0, 10];
  163. // const moreThanZero = numbers.some((val, index, array) => val > 0);
  164. // console.log(moreThanZero); //true
  165.  
  166. // const numbersAgain = [0, -1, -2];
  167. // const moreThanZeroAgain = numbersAgain.some((val, index, array) => val > 0);
  168. // console.log(moreThanZeroAgain); //false
  169.  
  170. // //Check if there is at least a country with less than 1000000 people
  171. // const population = [
  172. // {
  173. // country: 'China',
  174. // pop: 1409517397
  175. // },
  176. // {
  177. // country: 'India',
  178. // pop: 1339180127
  179. // },
  180. // {
  181. // country: 'USA',
  182. // pop: 324459463
  183. // },
  184. // {
  185. // country: 'Indonesia',
  186. // pop: 263991379
  187. // }
  188. // ];
  189.  
  190. // const check = population.some(val => val.pop < 1000000);
  191. // console.log(check); //false
  192.  
  193. //Check if there is a value more than 7
  194. const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  195. const moreThanSeven = numbers.find((val, index, array) => val > 7);
  196. console.log(moreThanSeven); //8
  197. //Check if there is a value more than 42
  198. const moreThanFortyTwo = numbers.find((val, index, array) => val > 42);
  199. console.log(moreThanFortyTwo); //undefined
  200.  
  201. //Check if there is a country with more than 100000000 people
  202. const population = [
  203. {
  204. country: 'China',
  205. pop: 1409517397
  206. },
  207. {
  208. country: 'India',
  209. pop: 1339180127
  210. },
  211. {
  212. country: 'USA',
  213. pop: 324459463
  214. },
  215. {
  216. country: 'Indonesia',
  217. pop: 263991379
  218. }
  219. ];
  220.  
  221. const check = population.find(val => val.pop > 100000000);
  222. console.log(check); //{ country: 'China', pop: 1409517397 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement