Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //forEach()
- // for loop
- for (let i = 0; i < companies.length; i++) {
- console.log(companies[i]);
- }
- // forEach()
- companies.forEach(function(company) {
- console.log(company);
- });
- // forEach() Arrow Function
- companies.forEach(company => console.log(company))
- // filter()
- // get 21 and older
- /* let canDrink = [];
- for (let i = 0; i < ages.length; i++) {
- if (ages[i] >= 21) {
- canDrink.push(ages[i]);
- }
- } */
- /* const canDrink = ages.filter(function(age) {
- if (age >= 21) {
- return true;
- }
- }); */
- const canDrink = ages.filter(age => age >= 21);
- // map()
- // Create array of company names
- const companyNames = companies.map(function(company) {
- return company.name;
- });
- console.log(companyNames);
- const agesSquare = ages.map(age => Math.sqrt(age));
- const agesTimesTwo = ages.map(age => age * 2);
- const ageMap = ages.map(age => Math.sqrt(age)).map(age => age * 2);
- // sort()
- // sort companies by start Year
- const sortedCompanies = companies.sort(function(c1, c2) {
- if (c1.start > c2.start) {
- return 1;
- } else {
- }
- return -1;
- });
- const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
- // Sort Ages
- const sortAges = ages.sort((a, b) => a - b);
- // Combine Methods
- const combined = ages
- .map(age => age * 2)
- .filter(age => age >= 40)
- .sort((a, b) => a - b)
- .reduce((a, b) => a + b, 0);
- console.log(combined);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement