|
| 1 | +// Get your shorts on - this is an array workout! |
| 2 | +// ## Array Cardio Day 1 |
| 3 | + |
| 4 | +// Some data we can work with |
| 5 | +const inventors = [ |
| 6 | + { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
| 7 | + { first: 'Isaac', last: 'Newton', year: 1543, passed: 1727 }, |
| 8 | + { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
| 9 | + { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, |
| 10 | + { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, |
| 11 | + { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, |
| 12 | + { first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, |
| 13 | + { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 }, |
| 14 | + { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 }, |
| 15 | + { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 }, |
| 16 | + { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 }, |
| 17 | + { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } |
| 18 | +]; |
| 19 | + |
| 20 | +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']; |
| 21 | + |
| 22 | +// Array.prototype.filter() |
| 23 | +// 1. Filter the list of inventors for those who were born in the 1500's |
| 24 | +const fifteen = inventors.filter(a => (a.year >= 1500 && a.year < 1600)); |
| 25 | +console.table(fifteen); |
| 26 | + |
| 27 | +// Array.prototype.map() |
| 28 | +// 2. Give us an array of the inventors first and last names |
| 29 | +const fullName = inventors.map((inventor => `捐贈者:${inventor.first} ${inventor.last}` )); |
| 30 | +console.log(fullName); |
| 31 | + |
| 32 | +// Array.prototype.sort() |
| 33 | +// 3. Sort the inventors by birthdate, oldest to youngest |
| 34 | +const birthdate = inventors.sort((a,b) => (b.year - a.year)); |
| 35 | +console.table(birthdate); |
| 36 | + |
| 37 | + |
| 38 | +// Array.prototype.reduce() |
| 39 | +// 4. How many years did all the inventors live all together? |
| 40 | +const totalYear = inventors.reduce((total,inventor) => (total + (inventor.passed - inventor.year)),0); |
| 41 | +console.log(totalYear); |
| 42 | + |
| 43 | +// 5. Sort the inventors by years lived |
| 44 | +const yearList = inventors.sort(function(a,b){ |
| 45 | + const prevInventor = a.passed - a.year; |
| 46 | + const nextInventor = b.passed - b.year; |
| 47 | + return prevInventor - nextInventor; |
| 48 | +}); |
| 49 | +console.table(yearList); |
| 50 | + |
| 51 | +// 列出所有歲數 |
| 52 | +const newYearList = yearList.map( inventor => inventor.passed - inventor.year); |
| 53 | +console.table(newYearList); |
| 54 | + |
| 55 | +// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 56 | +// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 57 | + |
| 58 | +const category = document.querySelector('.mw-category'); |
| 59 | +const links = Array.from(category.querySelectorAll('a')); |
| 60 | +const de = links |
| 61 | + .map(link => link.textContent) |
| 62 | + .filter(streetName => streetName.includes('de')); |
| 63 | +console.log(de); |
| 64 | + |
| 65 | +// 7. sort Exercise |
| 66 | +// Sort the "people" alphabetically by last name |
| 67 | +const lastNameList = people.sort(function(lastOne,nextOne){ |
| 68 | + const [aLast, aFirst] = lastOne.split(', '); |
| 69 | + const [bLast, bFirst] = nextOne.split(', '); |
| 70 | + return aLast > bLast ? 1 : -1; |
| 71 | + }); |
| 72 | +console.table(lastNameList); |
| 73 | + |
| 74 | +// 8. Reduce Exercise |
| 75 | +// Sum up the instances of each of these |
| 76 | +const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 77 | + |
| 78 | +const transportation = data.reduce(function(obj,item){ |
| 79 | + // if there is no object item, then object item is equal(等於) to zero. |
| 80 | + // console.log(item); |
| 81 | + if(!obj[item]){ |
| 82 | + obj[item] = 0; |
| 83 | + } |
| 84 | + obj[item]++; |
| 85 | + return obj; |
| 86 | +},{}); |
| 87 | + |
| 88 | +console.log(transportation); |
| 89 | + |
| 90 | + |
0 commit comments