|
31 | 31 |
|
32 | 32 | // Array.prototype.filter()
|
33 | 33 | // 1. Filter the list of inventors for those who were born in the 1500's
|
34 |
| - |
| 34 | + const bornIn1500 = inventors.filter( i => i.year >= 1500 && i.year < 1600 ) |
| 35 | + console.log( 'bornIn1500: ', bornIn1500 ); |
35 | 36 | // Array.prototype.map()
|
36 | 37 | // 2. Give us an array of the inventors' first and last names
|
| 38 | + const firstLastName = inventors.map( i => ( `${i.first} ${i.last}` ) ); |
| 39 | + console.log( 'firstLastName: ', firstLastName ); |
37 | 40 |
|
38 | 41 | // Array.prototype.sort()
|
39 | 42 | // 3. Sort the inventors by birthdate, oldest to youngest
|
40 |
| - |
| 43 | + const sort = inventors.sort( (a,b)=> a.year - b.year ) |
| 44 | + console.log( 'sort: ', sort ); |
41 | 45 | // Array.prototype.reduce()
|
42 | 46 | // 4. How many years did all the inventors live?
|
| 47 | + const reduce = inventors.reduce( (lived, i) => (i.passed - i.year) + lived, 0) |
| 48 | + console.log( 'reduce: ', reduce ); |
43 | 49 |
|
44 | 50 | // 5. Sort the inventors by years lived
|
| 51 | + const sortLived = inventors.sort( (a,b) => (a.passed - a.year) - (b.passed - b.year)) |
| 52 | + console.log( 'sortLived: ', sortLived ); |
45 | 53 |
|
46 | 54 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
47 | 55 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
| 56 | + // const links = document.querySelectorAll('.mw-category a'), |
| 57 | + // de = Array.prototype.map.call(links, a => a.innerText) |
| 58 | + // .filter(a => a.indexOf('de') !== -1 ) |
48 | 59 |
|
49 | 60 |
|
50 | 61 | // 7. sort Exercise
|
51 | 62 | // Sort the people alphabetically by last name
|
52 |
| - |
| 63 | + const peopleSort = people.sort((a,b) => a.split(',')[1] < b.split(',')[1] ? -1 : 1 ) |
| 64 | + console.log( 'peopleSort: ', peopleSort ); |
53 | 65 | // 8. Reduce Exercise
|
54 | 66 | // Sum up the instances of each of these
|
55 | 67 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
56 | 68 |
|
| 69 | + const dataReduce = data.reduce((obj, a) => { |
| 70 | + obj[a] = ! obj[a] ? 1 : obj[a] += 1; |
| 71 | + return obj; |
| 72 | + } , {} ) |
| 73 | + |
| 74 | + console.dir(dataReduce) |
| 75 | + |
57 | 76 | </script>
|
58 | 77 | </body>
|
59 | 78 | </html>
|
0 commit comments