|
44 | 44 |
|
45 | 45 | // Array.prototype.filter() |
46 | 46 | // 1. Filter the list of inventors for those who were born in the 1500's |
47 | | - // 筛选 16 世纪出生的人 |
| 47 | + // 1. 筛选 16 世纪出生的人 |
48 | 48 | const fifteen = inventors.filter(function (inventor) { |
49 | 49 | if (inventor.year >= 1500 && inventor.year < 1600) { |
50 | 50 | return true; |
51 | 51 | } |
52 | 52 | }); |
| 53 | + console.log('1. 筛选 16 世纪出生的人'); |
53 | 54 | console.table(fifteen); |
54 | 55 |
|
55 | 56 | const __fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
|
59 | 60 | // Array.prototype.map() |
60 | 61 | // 2. Give us an array of the inventors' first and last names |
61 | 62 | // 展示上面发明家的姓名 |
| 63 | + console.log('2. 展示上面发明家的姓名'); |
62 | 64 | const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
63 | 65 | // const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
64 | 66 | console.log(fullNames); |
|
67 | 69 | // Array.prototype.sort() |
68 | 70 | // 3. Sort the inventors by birthdate, oldest to youngest |
69 | 71 | // 把这些人从大到小进行排序 |
| 72 | + console.log('3. 把发明家从大到小进行排序'); |
70 | 73 | // const ordered = inventors.sort(function(firstName, secondName) { |
71 | 74 | // if(firstName.year > secondName.year) { |
72 | 75 | // return 1; // 对 sort 函数,返回值为 -1 排在前面,1 排在后面 |
|
76 | 79 | // }); |
77 | 80 | // console.table(ordered); |
78 | 81 |
|
79 | | - const __ordered = inventors.sort((a, b) => (a > b) ? 1 : -1); |
| 82 | + const __ordered = inventors.sort((a, b) => (a.year > b.year) ? 1 : -1); |
80 | 83 | console.table(__ordered); |
81 | 84 |
|
82 | 85 |
|
83 | 86 | // Array.prototype.reduce() |
84 | 87 | // 4. How many years did all the inventors live |
85 | 88 | // 他们所有人一共活了多少岁 |
| 89 | + console.log('4. 所有发明家一共活了多少岁'); |
| 90 | + |
86 | 91 | // 下面三种写法是一样的效果 |
87 | 92 | // var total = 0; |
88 | 93 | // for(var i = 0; i < inventors.length; i++) { |
|
102 | 107 |
|
103 | 108 | // 5. Sort the inventors by years lived、 |
104 | 109 | // 按照他们在世的岁数进行排序 |
105 | | - const oldest = people.sort((a, b) => { |
| 110 | + console.log('5. 按照发明家在世的岁数进行排序'); |
| 111 | + const oldest = inventors.sort((a, b) => { |
106 | 112 | const last = a.passed - a.year; |
107 | 113 | const next = b.passed - b.year; |
108 | 114 | return (next < last) ? -1 : 1; |
|
131 | 137 |
|
132 | 138 | // 7. sort Exercise |
133 | 139 | // Sort the people alphabetically by last name |
134 | | - // 按照姓氏的字母进行排序 |
| 140 | + // 按照姓氏(逗号后面的单词)的字母进行排序 |
| 141 | + console.log('7. 按照people姓氏(逗号后面的单词)的字母进行排序'); |
135 | 142 | const sortName = people.sort((a, b) => { |
136 | | - return (a.last> b.last) ? 1 : -1; |
| 143 | + return (a.split(', ')[1]> b.split(', ')[1]) ? 1 : -1; |
137 | 144 | }) |
138 | 145 | console.table(sortName); |
139 | 146 |
|
140 | 147 |
|
141 | 148 | // 8. Reduce Exercise |
142 | 149 | // Sum up the instances of each of these |
143 | 150 | // 统计各个物品的数量 |
| 151 | + console.log('8. 统计各个物品的数量'); |
144 | 152 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', |
145 | 153 | 'truck' |
146 | 154 | ]; |
|
0 commit comments