|
6 | 6 | </head>
|
7 | 7 | <body>
|
8 | 8 | <p><em>请按 F12 查看 Console 面板</em> 💁</p>
|
9 |
| - <p>若无结果请刷新试试</p> |
| 9 | + <p>若无结果请刷新试试</p> |
10 | 10 | <script>
|
11 |
| - |
12 | 11 | // ## Array 基础 Day 1
|
13 |
| - |
14 | 12 | // Some data we can work with
|
15 | 13 |
|
16 | 14 | const inventors = [
|
|
28 | 26 | { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
|
29 | 27 | ];
|
30 | 28 |
|
31 |
| - 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']; |
| 29 | + const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', |
| 30 | + 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', |
| 31 | + 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', |
| 32 | + 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry' |
| 33 | + ]; |
32 | 34 |
|
33 |
| - 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']; |
| 35 | + const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', |
| 36 | + 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', |
| 37 | + 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', |
| 38 | + 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', |
| 39 | + 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', |
| 40 | + 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', |
| 41 | + 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', |
| 42 | + 'Blair, Tony', 'Blake, William' |
| 43 | + ]; |
34 | 44 |
|
35 | 45 | // Array.prototype.filter()
|
36 | 46 | // 1. Filter the list of inventors for those who were born in the 1500's
|
37 |
| - // 筛选 16 世纪出生的人 |
38 |
| - const fifteen = inventors.filter(function(inventor) { |
39 |
| - if (inventor.year >= 1500 && inventor.year < 1600 ) { |
40 |
| - return true; |
41 |
| - } |
42 |
| - }); |
43 |
| - console.table(fifteen); |
44 |
| - |
45 |
| - const __fifteen = inventors.filter(inventor =>(inventor.year >= 1500 && inventor.year < 1600)); |
46 |
| - console.table(__fifteen); |
| 47 | + // 筛选 16 世纪出生的人 |
| 48 | + const fifteen = inventors.filter(function (inventor) { |
| 49 | + if (inventor.year >= 1500 && inventor.year < 1600) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + }); |
| 53 | + console.table(fifteen); |
| 54 | + |
| 55 | + const __fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
| 56 | + console.table(__fifteen); |
47 | 57 |
|
48 | 58 |
|
49 | 59 | // Array.prototype.map()
|
50 | 60 | // 2. Give us an array of the inventors' first and last names
|
51 |
| - // 展示上面发明家的姓名 |
52 |
| - const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
53 |
| -// const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
54 |
| - console.log(fullNames); |
55 |
| - |
| 61 | + // 展示上面发明家的姓名 |
| 62 | + const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
| 63 | + // const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 64 | + console.log(fullNames); |
| 65 | + |
56 | 66 |
|
57 | 67 | // Array.prototype.sort()
|
58 | 68 | // 3. Sort the inventors by birthdate, oldest to youngest
|
59 |
| - // 把这些人从大到小进行排序 |
60 |
| -// const ordered = inventors.sort(function(firstName, secondName) { |
61 |
| -// if(firstName.year > secondName.year) { |
62 |
| -// return 1; // 对 sort 函数,返回值为 -1 排在前面,1 排在后面 |
63 |
| -// } else { |
64 |
| -// return -1; |
65 |
| -// } |
66 |
| -// }); |
67 |
| -// console.table(ordered); |
68 |
| - |
69 |
| - const __ordered = inventors.sort((a, b) => (a > b) ? 1 : -1); |
70 |
| - console.table(__ordered); |
| 69 | + // 把这些人从大到小进行排序 |
| 70 | + // const ordered = inventors.sort(function(firstName, secondName) { |
| 71 | + // if(firstName.year > secondName.year) { |
| 72 | + // return 1; // 对 sort 函数,返回值为 -1 排在前面,1 排在后面 |
| 73 | + // } else { |
| 74 | + // return -1; |
| 75 | + // } |
| 76 | + // }); |
| 77 | + // console.table(ordered); |
| 78 | + |
| 79 | + const __ordered = inventors.sort((a, b) => (a > b) ? 1 : -1); |
| 80 | + console.table(__ordered); |
71 | 81 |
|
72 | 82 |
|
73 | 83 | // Array.prototype.reduce()
|
74 | 84 | // 4. How many years did all the inventors live
|
75 |
| - // 他们所有人一共活了多少岁 |
76 |
| - // 下面三种写法是一样的效果 |
77 |
| -// var total = 0; |
78 |
| -// for(var i = 0; i < inventors.length; i++) { |
79 |
| -// total += inventors[i].passed - inventors[i].year; |
80 |
| -// } |
81 |
| -// console.log(total); |
82 |
| -// |
83 |
| -// var totalYears = inventors.reduce(function(total, inventor) { |
84 |
| -// return total + inventor.passed - inventor.year; |
85 |
| -// }, 0); |
86 |
| -// console.log(totalYears); |
87 |
| - |
88 |
| - const totalYear = inventors.reduce( (total, inventor) => { |
89 |
| - return total + inventor.passed - inventor.year; |
90 |
| - }, 0); |
91 |
| - console.log(totalYear); |
| 85 | + // 他们所有人一共活了多少岁 |
| 86 | + // 下面三种写法是一样的效果 |
| 87 | + // var total = 0; |
| 88 | + // for(var i = 0; i < inventors.length; i++) { |
| 89 | + // total += inventors[i].passed - inventors[i].year; |
| 90 | + // } |
| 91 | + // console.log(total); |
| 92 | + // |
| 93 | + // var totalYears = inventors.reduce(function(total, inventor) { |
| 94 | + // return total + inventor.passed - inventor.year; |
| 95 | + // }, 0); |
| 96 | + // console.log(totalYears); |
| 97 | + |
| 98 | + const totalYear = inventors.reduce((total, inventor) => { |
| 99 | + return total + inventor.passed - inventor.year; |
| 100 | + }, 0); |
| 101 | + console.log(totalYear); |
92 | 102 |
|
93 | 103 | // 5. Sort the inventors by years lived、
|
94 |
| - // 按照他们在世的岁数进行排序 |
95 |
| - const oldest = inventors.sort( (a, b) => { |
96 |
| - const last = a.passed - a.year; |
97 |
| - const next = b.passed - b.year; |
98 |
| - return (next < last) ? -1 : 1; |
99 |
| - }); |
100 |
| - console.table(oldest); |
| 104 | + // 按照他们在世的岁数进行排序 |
| 105 | + const oldest = people.sort((a, b) => { |
| 106 | + const last = a.passed - a.year; |
| 107 | + const next = b.passed - b.year; |
| 108 | + return (next < last) ? -1 : 1; |
| 109 | + }); |
| 110 | + console.table(oldest); |
101 | 111 |
|
102 | 112 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
103 | 113 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
104 |
| - |
| 114 | + |
105 | 115 | // 这个是超级酷的一个玩法,打开上面的网页。然后输入下面的语句,就可以筛选出页面中含有 'de' 这个词的所有街道的名称
|
106 |
| - |
| 116 | + |
107 | 117 | // const category = document.querySelector('.mw-category');
|
108 | 118 | // const links = Array.from(category.querySelectorAll('a'));
|
109 | 119 | // const de = links
|
110 | 120 | // .map(link => link.textContent)
|
111 | 121 | // .filter(streetName => streetName.includes('de'));
|
112 | 122 |
|
113 |
| - |
114 |
| - // 下面是我在豆瓣里筛选书名里含有 CSS 的书的代码 |
115 |
| - // https://book.douban.com/tag/web |
116 |
| -// const links = Array.from(document.querySelectorAll('.subject-list h2 a')); |
117 |
| -// const book = links |
118 |
| -// .map(link => link.title) |
119 |
| -// .filter(title => title.includes('CSS')); |
120 |
| - |
121 |
| - |
| 123 | + |
| 124 | + // 下面是我在豆瓣里筛选书名里含有 CSS 的书的代码 |
| 125 | + // https://book.douban.com/tag/web |
| 126 | + // const links = Array.from(document.querySelectorAll('.subject-list h2 a')); |
| 127 | + // const book = links |
| 128 | + // .map(link => link.title) |
| 129 | + // .filter(title => title.includes('CSS')); |
| 130 | + |
| 131 | + |
122 | 132 | // 7. sort Exercise
|
123 | 133 | // Sort the people alphabetically by last name
|
124 |
| - // 按照姓氏的字母进行排序 |
125 |
| - const sortName = inventors.sort( (a, b) => { |
126 |
| - return (a.last > b.last) ? 1 : -1; |
127 |
| - }) |
128 |
| - console.table(sortName); |
129 |
| - |
130 |
| - |
| 134 | + // 按照姓氏的字母进行排序 |
| 135 | + const sortName = inventors.sort((a, b) => { |
| 136 | + return (a.last > b.last) ? 1 : -1; |
| 137 | + }) |
| 138 | + console.table(sortName); |
| 139 | + |
| 140 | + |
131 | 141 | // 8. Reduce Exercise
|
132 | 142 | // Sum up the instances of each of these
|
133 |
| - // 统计各个物品的数量 |
134 |
| - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
135 |
| - const reduce = data.reduce( (obj, item) => { |
136 |
| - if( !obj[item] ) { |
137 |
| - obj[item] = 0; |
138 |
| - } |
139 |
| - obj[item]++; |
140 |
| - return obj; |
141 |
| - }, {}); |
142 |
| - console.log(reduce); |
143 |
| - |
| 143 | + // 统计各个物品的数量 |
| 144 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', |
| 145 | + 'truck' |
| 146 | + ]; |
| 147 | + const reduce = data.reduce((obj, item) => { |
| 148 | + if (!obj[item]) { |
| 149 | + obj[item] = 0; |
| 150 | + } |
| 151 | + obj[item]++; |
| 152 | + return obj; |
| 153 | + }, {}); |
| 154 | + console.log(reduce); |
144 | 155 | </script>
|
145 | 156 | </body>
|
146 | 157 | </html>
|
0 commit comments