<script>
// Array.prototype.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
const bornIn1500 = inventors.filter((v) => v.year >= 1500 && v.year < 1600)
console.log(bornIn1500)
// Array.prototype.map()
// 2. Give us an array of the inventors first and last names
const names = inventors.map(v => "" + v.first + " " + v.last)
console.log(names)
// Array.prototype.sort()
// 3. Sort the inventors by birthdate, oldest to youngest
const age = inventors.sort((a,b) => a.year - b.year)
console.log(age)
// Array.prototype.reduce()
// 4. How many years did all the inventors live all together?
const totalYears = inventors.reduce((acc, inventor) => {
return acc + (inventor.passed - inventor.year)
}, 0 )
console.log(totalYears)
// 5. Sort the inventors by years lived
const yearsLived = inventors.sort((a,b) => (b.passed - b.year) - (a.passed - a.year))
console.log(yearsLived)
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
// const category = document.querySelector('.mw-category');
// const links = [...ategory.querySelectorAll('a')];
// const de = links
// .map(link => link.textContent)
// .filter(streetName => streetName.includes('de'));
// 7. sort Exercise
// Sort the people alphabetically by last name
const sortedName = people.sort((a,b) => {return a.split(', ')[0] > b.split(', ')[0] ? 1 : -1})
console.log(sortedName)
// 8. Reduce Exercise
// Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
const sumInstances = data.reduce((acc, value) => {
if (acc[value] === undefined) acc[value] = 1
else acc[value] += 1
return acc
}, {})
console.log(sumInstances)
</script>
<script>
// Array.prototype.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
console.table(fifteen);
// Array.prototype.map()
// 2. Give us an array of the inventor first and last names
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
console.log(fullNames);
// Array.prototype.sort()
// 3. Sort the inventors by birthdate, oldest to youngest
// const ordered = inventors.sort(function(a, b) {
// if(a.year > b.year) {
// return 1;
// } else {
// return -1;
// }
// });
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
console.table(ordered);
// Array.prototype.reduce()
// 4. How many years did all the inventors live?
const totalYears = inventors.reduce((total, inventor) => {
return total + (inventor.passed - inventor.year);
}, 0);
console.log(totalYears);
// 5. Sort the inventors by years lived
const oldest = inventors.sort(function(a, b) {
const lastInventor = a.passed - a.year;
const nextInventor = b.passed - b.year;
return lastInventor > nextInventor ? -1 : 1;
});
console.table(oldest);
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
// const category = document.querySelector('.mw-category');
// const links = Array.from(category.querySelectorAll('a'));
// const de = links
// .map(link => link.textContent)
// .filter(streetName => streetName.includes('de'));
// 7. sort Exercise
// Sort the people alphabetically by last name
const alpha = people.sort((lastOne, nextOne) => {
const [aLast, aFirst] = lastOne.split(', ');
const [bLast, bFirst] = nextOne.split(', ');
return aLast > bLast ? 1 : -1;
});
console.log(alpha);
// 8. Reduce Exercise
// Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick'];
const transportation = data.reduce(function(obj, item) {
if (!obj[item]) {
obj[item] = 0;
}
obj[item]++;
return obj;
}, {});
console.log(transportation);
</script>
JS๋ก ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ๋ฉด์ ์ต์ํด์ก๋ map, reduce๋ฑ์ ๋ค์ ์ ๋ฆฌํด๋ณผ ์ ์๋ ์๊ฐ์ด์๋ค.
sort๋ ์ข ๋ ๊ณต๋ถํ๊ณ ๋์ ๋ธ๋ก๊ทธ์ ์ ๋ฆฌ๋ฅผ ๋ฐ๋ก ํด์ผํ ๊ฒ ๊ฐ๋ค.