Skip to content

Instantly share code, notes, and snippets.

@whal-e3
Last active November 10, 2020 13:25
Show Gist options
  • Save whal-e3/41aa93bdbcb94a08cded65f758aa1e02 to your computer and use it in GitHub Desktop.
Save whal-e3/41aa93bdbcb94a08cded65f758aa1e02 to your computer and use it in GitHub Desktop.
forEach, filter, map, sort, reduce
// This file is used in youtube video "Traversy Media".
// visit "Traversy Media" down below
// https://www.youtube.com/watch?v=rRgD1yVwIvE&list=PLillGF-RfqbbnEGy3ROiLWk7JMCuSyQtX&index=9
const companies= [
{name: "Company One", category: "Finance", start: 1981, end: 2004},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
// forEach
Get company names
for(let i = 0; i < companies.length; i++){
console.log(companies[i]);
}
companies.forEach(function(company){
console.log(company.name);
})
//-------------------------------------------------------------------
// filter
// Get 21 or 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);
console.log(canDrink);
// Retail companies
const retailCompanies = companies.filter(function(company){
if(company.category === 'Retail'){
return true;
}
});
const retailCompanies = companies.filter(company => company.category === 'Retail');
console.log(retailCompanies);
// Get 80s companies
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
console.log(eightiesCompanies);
// Get compnies lasted 10 years+
const last10years = companies.filter(company => (company.end - company.start >= 10));
console.log(last10years);
//-------------------------------------------------------------------
// map
// Create array of company names
const companyNames = companies.map(function(company){
return `${company.name} [${company.start} - ${company.end}]`;
});
const companyNames = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
console.log(companyNames);
// Modify ages
const ageMap = ages
.map(age => Math.sqrt(age))
.map(age => age * 2);
console.log(ageMap);
//-------------------------------------------------------------------
// 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));
const sortedCompanies = companies.sort((a, b) => (a.start - b.start));
console.log(sortedCompanies);
// Sort ages
const sortAges = ages.sort((a, b) => a - b);
console.log(sortAges);
//-------------------------------------------------------------------
// reduce
// Sum all ages
let ageSum = 0;
for(let i = 0; i < ages.length; i++) {
ageSum += ages[i];
}
const ageSum = ages.reduce(function(total, age) {
return total + age;
}, 0);
const ageSum = ages.reduce((total, age) => (total + age), 0);
console.log(ageSum);
// Get total years for all companies
const totalYears = companies.reduce(function(total, company){
return total + (company.end - company.start);
}, 0);
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
console.log(totalYears);
//-------------------------------------------------------------------
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment