1. 程式人生 > >【前端】陣列資料操縱:map, filter, sort, reduce

【前端】陣列資料操縱:map, filter, sort, reduce

const companies = [
	{ name: "Company One", category: "Finance", start: 1981, end: 2003 }, 
	{ name: "Company Two", category: "Retail", start: 1991, end: 2016 }, 
	{ name: "Company Three", category: "Auto", start: 1983, end: 2007 }, 
	{ name: "Company Four", category: "Retail", start: 1987, end: 2009 }, 
	{
name: "Company Five", category: "Tech", start: 1985, end: 2010 }, { name: "Company Six", category: "Finance", start: 1988, end: 2000 }, { name: "Company Seven", category: "Auto", start: 1986, end: 2012 }, { name: "Company Eight", category: "Retail", start: 1984, end: 2005 }, { name: "Company Nine",
category: "Finance", start: 1998, end: 2013 }, ]; const ages = [23, 5, 26, 25, 22, 25, 22, 23, 22, 15]; // for(let i = 0; i < companies.length; i++) { // console.log(companies[i]) // } // forEach // companies.forEach(function(company) { // console.log(company.name); // }); // filter // 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) // fiter retail companies // const retailCompanies = companies.filter(function(company) { // if(company.category === "Retail") { // return true; // } // }); // const retailCompanies = companies.filter( company => (company.category === "Retail" && company.start > 1990)); // console.log(retailCompanies); // get 80s companies // const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990)); // map : 建立新的array // const companyNames = companies.map(function(company){ // return company.name; // }); // const testMap = companies.map(function(company) { // return `${company.name} [${company.end} - ${company.start}]`; // }); // const testMap = companies.map(company => `${company.name} [${company.end} - ${company.start}]`); // console.log(testMap); // sort // Sort companies by start year // const sortedCompanies = companies.sort(function(a, b) { // if(a.start > b.start) { // return 1; // 升序 // } else { // return -1; // } // }); // 等價寫法 const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1)); // console.log(sortedCompanies); // sort ages // const sortedAges = ages.sort((a,b) => (a > b ? 1 : -1)) // 等價寫法 const sortedAges = ages.sort((a,b) => (a - b)) // console.log(sortedAges) // reduce // 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) // get total years of 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); // 綜合來看 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);

END.