Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
- // cities.forEach((value, index, array) => {
- // console.log(`${index + 1} ${value}`); //output: 1 ROME, 2 PARIS, 3 LONDON, 4 LOS ANGELES, 5 VIENNA
- // });
- // //we can use it to invert the "cities" array...even though with reverse() would be better 😉
- // const invertedCities = [];
- // cities.forEach((value, index, array) => invertedCities.unshift(value));
- // console.log(invertedCities); //output: ["VIENNA", "LOS ANGELES", "LONDON", "PARIS", "ROME"]
- //Let's create a new array with all our numbers squared
- // const numbers = [1, 2, 3, 4, 5];
- // const squaredNumbers = numbers.map(number => number * number);
- // console.log(squaredNumbers); //output: [1, 4, 9, 16, 25]
- // //We all know which is the most beautiful city in the World... 😉
- // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
- // const bestCity = cities.map(city => (city === 'ROME' ? city : 'ROME'));
- // console.log(bestCity); //output: ["ROME", "ROME", "ROME", "ROME", "ROME"]
- // //Let's create an array of HTML tag
- // const html = cities.map(city => `<li>${city}</li>`);
- // console.log(html); //output: ["<li>ROME</li>", "<li>PARIS</li>", "<li>LONDON</li>", "<li>LOS ANGELES</li>", "<li>VIENNA</li>"]
- // //Transform an array of strings in an array of objects
- // const metalBands = ['IRON MAIDEN', 'SLAYER', 'JUDAS PRIEST'];
- // const collection = metalBands.map((band, index) => {
- // let obj = {}; //create an empty object at any call of the loop
- // obj.id = index; //create a key called "id" and set it equal to our index parameter
- // obj.band = band; //create a key called "band" and set it equal to our band parameter
- // return obj; //return an object at any call with key/value pairs like this: {id: 'index', band: 'band-name'}
- // });
- // console.log(collection); //output: [{id: 0, band: "IRON MAIDEN"},{id: 1, band: "SLAYER"}, {id: 2, band: "JUDAS PRIEST"}]
- // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- // //Return an array of even values from numbers
- // const evens = numbers.filter(num => num % 2 === 0);
- // console.log(evens); //output: [2, 4, 6, 8, 10]
- // //Return an array of odd values from numbers
- // const odds = numbers.filter(num => num % 2 !== 0);
- // console.log(odds); //output: [1, 3, 5, 7, 9]
- // //All the roads lead to Rome 🚋 ...
- // const cities = ['ROME', 'PARIS', 'LONDON', 'LOS ANGELES', 'VIENNA'];
- // const rome = cities.filter(city => city == 'ROME');
- // console.log(rome); //output: ["ROME"]
- // //You can chain together this methods 🚀🚀🚀
- // //Example: let's square all even numbers
- // const squaredEvens = numbers.filter(num => num % 2 === 0).map(num => num * num);
- // console.log(squaredEvens); //output: [4, 16, 36, 64, 100];
- // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- // //Let's start with a basic example
- // //Sum all the numbers in an array of integers
- // const sum = numbers.reduce((accumulator, currentValue, currentIndex, array) => {
- // //Look we set the Initial Value to 0, because it is a sum so the count starts at 0
- // //So our Accumulator is 0 at first call and we sum it with the Current Value that is 1 at first call...
- // //the new Accumulator will be 0 + 1 = 1 ...at any call the Current Value will be added to it
- // //till the end of the array
- // return accumulator + currentValue;
- // }, 0);
- // console.log(sum); // output: 55
- // //Same example setting the Initial Value to 1 instead of 0 will return ... 56
- // const anotherSum = numbers.reduce((accumulator, currentValue, currentIndex, array) => {
- // return accumulator + currentValue;
- // }, 1);
- // console.log(anotherSum); // output: 56
- // //Sum from an array of objects
- // const integers = [{ x: 1 }, { x: 2 }, { x: 3 }];
- // const anotherSumAgain = integers.reduce((acc, val, idx, array) => {
- // return acc + val.x;
- // }, 0);
- // console.log(anotherSumAgain); // output: 6
- // //Count vowels in a string (even though it's easier with regex 😉)
- // const maryPoppins = 'supercalifragilisticexpialidocious';
- // const onlyVowels = maryPoppins.replace(/[^aeiou]/gi, ''); //'ueaiaiiieiaioiou'
- // const arrOfVowels = [...onlyVowels]; //["u", "e", "a", "i", "a", "i", "i", "i", "e", "i", "a", "i", "o", "i", "o", "u"]
- // const countVowels = arrOfVowels.reduceRight((acc, val) => {
- // acc.hasOwnProperty(val) ? (acc[val] += 1) : (acc[val] = 0);
- // return acc;
- // }, {});
- // console.log(countVowels); // output: {u: 1, e: 1, a: 2, i: 6, o: 1}
- // //Flatten an array of arrays
- // //Hey! I know ES2019 gave us flat and flatMap methods, but we MUST learn reduce() now 😉
- // const someData = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
- // const flatten = someData.reduce((acc, val) => {
- // //set the initial value to an empty array
- // return acc.concat(val);
- // }, []);
- // console.log(flatten); // output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
- // //Sum all countries population except China
- // const population = [
- // {
- // country: 'China',
- // pop: 1409517397
- // },
- // {
- // country: 'India',
- // pop: 1339180127
- // },
- // {
- // country: 'USA',
- // pop: 324459463
- // },
- // {
- // country: 'Indonesia',
- // pop: 263991379
- // }
- // ];
- // const sumPopulationButNotChina = population.reduce((acc, val) => {
- // // we use the Ternary Operator as a "filter"
- // //if val.country is not equal to "China" we add the value to the accumulator
- // //if it is equal to "China" we add nothing and simply return the accumulator
- // return val.country !== 'China' ? acc + val.pop : acc;
- // }, 0);
- // console.log(sumPopulationButNotChina); // output: 1927630969
- //Check if all values are more than zero
- // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- // const moreThanZero = numbers.every((val, index, array) => val > 0);
- // console.log(moreThanZero); //true
- // const numbersAgain = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- // const moreThanZeroAgain = numbersAgain.every((val, index, array) => val > 0);
- // console.log(moreThanZeroAgain); //false
- // //Check if there are more than 1000000 people in all the countries
- // const population = [
- // {
- // country: 'China',
- // pop: 1409517397
- // },
- // {
- // country: 'India',
- // pop: 1339180127
- // },
- // {
- // country: 'USA',
- // pop: 324459463
- // },
- // {
- // country: 'Indonesia',
- // pop: 263991379
- // }
- // ];
- // const check = population.every(val => val.pop > 1000000);
- // console.log(check); //true
- //Check if a value is more than zero in the array
- // const numbers = [-1, -2, 0, 10];
- // const moreThanZero = numbers.some((val, index, array) => val > 0);
- // console.log(moreThanZero); //true
- // const numbersAgain = [0, -1, -2];
- // const moreThanZeroAgain = numbersAgain.some((val, index, array) => val > 0);
- // console.log(moreThanZeroAgain); //false
- // //Check if there is at least a country with less than 1000000 people
- // const population = [
- // {
- // country: 'China',
- // pop: 1409517397
- // },
- // {
- // country: 'India',
- // pop: 1339180127
- // },
- // {
- // country: 'USA',
- // pop: 324459463
- // },
- // {
- // country: 'Indonesia',
- // pop: 263991379
- // }
- // ];
- // const check = population.some(val => val.pop < 1000000);
- // console.log(check); //false
- //Check if there is a value more than 7
- const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- const moreThanSeven = numbers.find((val, index, array) => val > 7);
- console.log(moreThanSeven); //8
- //Check if there is a value more than 42
- const moreThanFortyTwo = numbers.find((val, index, array) => val > 42);
- console.log(moreThanFortyTwo); //undefined
- //Check if there is a country with more than 100000000 people
- const population = [
- {
- country: 'China',
- pop: 1409517397
- },
- {
- country: 'India',
- pop: 1339180127
- },
- {
- country: 'USA',
- pop: 324459463
- },
- {
- country: 'Indonesia',
- pop: 263991379
- }
- ];
- const check = population.find(val => val.pop > 100000000);
- console.log(check); //{ country: 'China', pop: 1409517397 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement