11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
4- < meta charset ="UTF-8 ">
5- < title > Array Cardio 💪</ title >
5+ < meta charset ="UTF-8 ">
6+ < title > Array Cardio 💪</ title >
67</ head >
8+
79< body >
8- < p > < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
9- < script >
10- // Get your shorts on - this is an array workout!
11- // ## Array Cardio Day 1
10+ < p > < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
11+ < script >
12+ // Get your shorts on - this is an array workout!
13+ // ## Array Cardio Day 1
14+
15+ // Some data we can work with
1216
13- // Some data we can work with
17+ const inventors = [ {
18+ first : 'Albert' ,
19+ last : 'Einstein' ,
20+ year : 1879 ,
21+ passed : 1955
22+ } , {
23+ first : 'Isaac' ,
24+ last : 'Newton' ,
25+ year : 1643 ,
26+ passed : 1727
27+ } , {
28+ first : 'Galileo' ,
29+ last : 'Galilei' ,
30+ year : 1564 ,
31+ passed : 1642
32+ } , {
33+ first : 'Marie' ,
34+ last : 'Curie' ,
35+ year : 1867 ,
36+ passed : 1934
37+ } , {
38+ first : 'Johannes' ,
39+ last : 'Kepler' ,
40+ year : 1571 ,
41+ passed : 1630
42+ } , {
43+ first : 'Nicolaus' ,
44+ last : 'Copernicus' ,
45+ year : 1473 ,
46+ passed : 1543
47+ } , {
48+ first : 'Max' ,
49+ last : 'Planck' ,
50+ year : 1858 ,
51+ passed : 1947
52+ } , {
53+ first : 'Katherine' ,
54+ last : 'Blodgett' ,
55+ year : 1898 ,
56+ passed : 1979
57+ } , {
58+ first : 'Ada' ,
59+ last : 'Lovelace' ,
60+ year : 1815 ,
61+ passed : 1852
62+ } , {
63+ first : 'Sarah E.' ,
64+ last : 'Goode' ,
65+ year : 1855 ,
66+ passed : 1905
67+ } , {
68+ first : 'Lise' ,
69+ last : 'Meitner' ,
70+ year : 1878 ,
71+ passed : 1968
72+ } , {
73+ first : 'Hanna' ,
74+ last : 'Hammarström' ,
75+ year : 1829 ,
76+ passed : 1909
77+ } ] ;
1478
15- const inventors = [
16- { first : 'Albert' , last : 'Einstein' , year : 1879 , passed : 1955 } ,
17- { first : 'Isaac' , last : 'Newton' , year : 1643 , passed : 1727 } ,
18- { first : 'Galileo' , last : 'Galilei' , year : 1564 , passed : 1642 } ,
19- { first : 'Marie' , last : 'Curie' , year : 1867 , passed : 1934 } ,
20- { first : 'Johannes' , last : 'Kepler' , year : 1571 , passed : 1630 } ,
21- { first : 'Nicolaus' , last : 'Copernicus' , year : 1473 , passed : 1543 } ,
22- { first : 'Max' , last : 'Planck' , year : 1858 , passed : 1947 } ,
23- { first : 'Katherine' , last : 'Blodgett' , year : 1898 , passed : 1979 } ,
24- { first : 'Ada' , last : 'Lovelace' , year : 1815 , passed : 1852 } ,
25- { first : 'Sarah E.' , last : 'Goode' , year : 1855 , passed : 1905 } ,
26- { first : 'Lise' , last : 'Meitner' , year : 1878 , passed : 1968 } ,
27- { first : 'Hanna' , last : 'Hammarström' , year : 1829 , passed : 1909 }
28- ] ;
79+ // 1.筛选出出生在16世纪的科学家
2980
30- const flavours = [ 'Chocolate Chip' , 'Kulfi' , 'Caramel Praline' , 'Chocolate' , 'Burnt Caramel' , 'Pistachio' , 'Rose' , 'Sweet Coconut' , 'Lemon Cookie' , 'Toffeeness' , 'Toasted Almond' , 'Black Raspberry Crunch' , 'Chocolate Brownies' , 'Pistachio Almond' , 'Strawberry' , 'Lavender Honey' , 'Lychee' , 'Peach' , 'Black Walnut' , 'Birthday Cake' , 'Mexican Chocolate' , 'Mocha Almond Fudge' , 'Raspberry' ] ;
81+ const flavours = [ 'Chocolate Chip' , 'Kulfi' , 'Caramel Praline' , 'Chocolate' , 'Burnt Caramel' , 'Pistachio' , 'Rose' , 'Sweet Coconut' , 'Lemon Cookie' , 'Toffeeness' , 'Toasted Almond' , 'Black Raspberry Crunch' , 'Chocolate Brownies' , 'Pistachio Almond' , 'Strawberry' , 'Lavender Honey' , 'Lychee' , 'Peach' , 'Black Walnut' , 'Birthday Cake' , 'Mexican Chocolate' , 'Mocha Almond Fudge' , 'Raspberry' ] ;
3182
32- 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' ] ;
83+ 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' ] ;
3384
34- // Array.prototype.filter()
35- // 1. Filter the list of inventors for those who were born in the 1500's
85+ // Array.prototype.filter() 筛选出出生在16世纪的科学家
86+ // 1. Filter the list of inventors for those who were born in the 1500's
87+ const filterArray =
88+ inventors . filter ( item => {
89+ if ( item . year > 1500 && item . year < 1600 ) {
90+ return true
91+ } else {
92+ return false
93+ }
94+ } )
95+ console . table ( filterArray )
3696
37- // Array.prototype.map()
38- // 2. Give us an array of the inventors' first and last names
97+ // Array.prototype.map() 输入科学家的名字
98+ // 2. Give us an array of the inventors' first and last names
99+ const mapArray = inventors . map ( item => item . first + ' ' + item . last )
100+ console . table ( mapArray )
39101
40- // Array.prototype.sort()
41- // 3. Sort the inventors by birthdate, oldest to youngest
102+ // Array.prototype.sort()
103+ // 3. Sort the inventors by birthdate, oldest to youngest
104+ const ordered = inventors . sort ( ( a , b ) => {
105+ return a . year > b . year ? 1 : - 1
106+ } )
107+ console . table ( ordered )
42108
43- // Array.prototype.reduce()
44- // 4. How many years did all the inventors live?
45109
46- // 5. Sort the inventors by years lived
110+ // Array.prototype.reduce()
111+ // 4. How many years did all the inventors live?
47112
48- // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
49- // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
113+ // 5. Sort the inventors by years lived
50114
115+ const liveLonger = inventors . sort ( ( a , b ) => ( ( a . passed - a . year ) > ( b . passed - b . year ) ) ? 1 : - 1 )
116+ console . table ( liveLonger )
51117
52- // 7. sort Exercise
53- // Sort the people alphabetically by last name
118+ // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
119+ // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
120+ // 筛选出含有'de'的标题
121+ // const category = document.querySelector('.mw-category')
122+ // const links = Array.from(category.querySelector('a'))
123+ // const de = links.map(item => item.textContent).filter(title => title.includes('de'))
54124
55- // 8. Reduce Exercise
56- // Sum up the instances of each of these
57- const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
58125
59- </ script >
126+ // 7. sort Exercise
127+ // Sort the people alphabetically by last name
128+ // const sortByLastName = inventors.sort((a, b) => a.last > b.last ? 1 : -1)
129+ // console.table(sortByLastName)
130+ const sortByLastName = people . sort ( ( a , b ) => {
131+ const [ afirst , alast ] = a . split ( ', ' )
132+ const [ bfirst , blast ] = b . split ( ', ' )
133+ console . log ( [ afirst , alast ] )
134+ return alast > blast ? 1 : - 1
135+ } )
136+ console . table ( sortByLastName )
137+
138+
139+ // 8. Reduce Exercise
140+ // Sum up the instances of each of these
141+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
142+ const static = data . reduce ( ( obj , item ) => {
143+ if ( ! obj [ item ] ) {
144+ obj [ item ] = 0
145+ }
146+ obj [ item ] ++ ;
147+ return obj
148+ } , { } )
149+ </ script >
60150</ body >
61- </ html >
151+
152+ </ html >
0 commit comments