DAY 6
1
Modern Web

## Day06: 針對陣列的操作練習(二)

6.Sort the inventors by years lived
7.create a list of Boulevards in Paris
that contain 'de' anywhere in the name
8.sort Exercise
9.Reduce Exercise

5.先算出年紀後再做 sort()
Sort the inventors by years lived

``````const oldest = inventors.sort((a, b) => ((a.passed - a.year) - (b.passed - b.year)))
console.log(oldest)
``````

6.create a list of Boulevards in Paris that contain ‘de’ anywhere in the name

``````let peoples = document.querySelectorAll('.mw-category-group a');
let array = Array.from(peoples)
let newList = array.map(item => {return item.innerHTML})
let result = newList.filter(name=>{
return name.includes('de')
})
``````

7.Sort the people alphabetically by last name

aLast = 'Beck' , aFirst = 'Glenn'
bLast = 'Becker' , bFirst = 'Carl'

• 回傳 1 即 aLast字母順序在 bLast 之後
• 回傳 -1 即 aLast字母順序在 bLast 之前
• 回傳 0 即 aLast字母順序與 bLast 一樣
``````const alpha = people.sort((a, b) => {
const [aLast, aFirst] = a.split(', ');
const [bLast, bFirst] = b.split(', ');
return aLast > bLast ? 1 : bLast > aLast ? -1 : 0;
});
console.log(alpha);
``````

8.Sum up the instances of each of these

``````const transportation = data.reduce((obj, item) => {

if (item in obj) {
obj[item]++;
}
else {
obj[item] = 1;
}
return obj;
}, {});

console.log(transportation);
``````

codePen

javascript30