Skip to content

Instantly share code, notes, and snippets.

@matherton
Last active November 26, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matherton/6b374fcadda1002420177f09f261ba8c to your computer and use it in GitHub Desktop.
Save matherton/6b374fcadda1002420177f09f261ba8c to your computer and use it in GitHub Desktop.
es6 Higher Order Functions & Arrays example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- The page title that is displayed in the top of the browser -->
<title></title>
<!-- Meta content describes your page and is indexed by search engines such as Google -->
<meta name="description" content="JS High Order function">
<meta name="author" content="Mark Atherton">
<!-- meta viewport ensures content is full screen width on mobile devises -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- link to external Cascading Style Sheet
<link rel="stylesheet" href="css/styles.css?v=1.0"> -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<!-- Opening body tag this where all your page content goes -->
<body>
<h1>company data in the console</h1>
<!-- Link to external JavaScript file -->
<script src="main.js"></script>
</body>
</html>
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];
/*ES5 way to use filter
financeCompanies = companies.filter(function(company) {
if(company.category === "Finance") {
return true;
}
});*/
const financeCompanies = companies.filter(company => company.category === "Finance");
//console.log(financeCompanies);
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.end < 1990));
//console.log(eightiesCompanies)
const lasted10Years = companies.filter(company => (company.end - company.start >= 10))
//console.log(lasted10Years)
const company = companies.map(function(company) {
return company.name
})
//map filter returning a template literal
const companyNames = companies.map(company => `${company.name} [${company.start} - ${company.end}]`)
//console.log(companyNames)
const agesSquared = ages.map(age => Math.sqrt(age))
//console.log(agesSquared)
//sort array via start date
/*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))
//console.log(sortedCompanies)
const sortAges = ages.sort((a, b) => a - b);
//console.log(sortAges);
// reduce
const ageSum = ages.reduce( (total, age) => total + age, 0);
//console.log(ageSum)
const allYears = companies.reduce(function(total, company) {
return total + (company.end - company.start);
}, 0)
console.log(allYears);
//The above can be written like:
//Get total years for all companies
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0)
console.log(totalYears)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment