iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 5
1
Modern Web

實作經典 JavaScript 30系列 第 5

Day05: 針對陣列的操作練習(一)

WES BOS系列影片
Alex快速導讀系列影片

開始前建議可以先讀這篇卡斯伯的大補帖
https://wcc723.github.io/javascript/2017/06/29/es6-native-array/

初步練習目標 :
1.filter()
2.map()
3.sort()
4.reduce()


資料結構:

const inventors = [
  { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
  { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
  { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
  { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
  { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
  { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
  { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
  { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
  { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
  { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
  { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
  { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];

1.filter()
Filter the list of inventors for those who were born in the 1500's
目標:取得1500~1600年代出生的發明家。

const fifteen = inventors.filter(inventor => (inventor.year > 1500 && inventor.year < 1600))
console.table(fifteen)

2.map()
Give us an array of the inventors first and last names
目標:製作一筆資料,只要發明家的姓名。

const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`)
console.log(fullNames)

3.sort()
Sort the inventors by birthdate, oldest to youngest
目標:根據發明家的生日日期,由老到年輕排列。

const ordered = inventors.sort((a, b) => a.year - b.year);
console.table(ordered)

sort 可以 比較兩個丟進去的參數,總共需要判斷3種狀況,就能讓陣列由小到大排序

  • a-b < 0 即 a比b小,所以a往前排
  • a-b = 0 即 a = b,所以不動
  • a-b > 0 即 a比b大,所以a往後排

4.reduce()
How many years did all the inventors live?
目標:這些發明家總共活了多少年。
可以先用forEach來了解基礎的加總方法

let totalYears = 0
inventors.forEach(inventor => totalYears += inventor.passed-inventor.year)
console.log(totalYears)

用reduce()再做一次

const totalYears = inventors.reduce((total, inventor) => {
  return total + (inventor.passed - inventor.year);
}, 0);
console.log(totalYears)

這幾個都是很常用到的陣列處理,
對於資料的整理會非常有幫助喔。
明天再繼續練習~


上一篇
Day04:用CSS + JS 做一個時鐘
下一篇
Day06: 針對陣列的操作練習(二)
系列文
實作經典 JavaScript 3030

尚未有邦友留言

立即登入留言