array 메소드에 대해 다룸
filter, map, sort, reduce 등을 이용해 주어진 문제를 해결
array 메소드들에 대한 이해
map
메소드가 없으면 유사배열 객체나 반복가능한 객체를 얕게 복사해 새로운 Array 객체로 만들어 순회할 수 있도록 배열형태로 반환해야한다 spread인 [...]
, Array.from()
을 사용해주도록 하자.NodeList는 Array와 다름
Array.isArray
메서드로 배열인지 확인할 수 있음 이는 유사배열임\1. Filter the list of inventors for those who were born in the 1500's ~ 1599
\2. Give us an array of the inventors first and last names
\3. Sort the inventors by birthdate, oldest to youngest
\4. How many years did all the inventors live all together?
https://*en.wikipedia.org/wiki/Category:Boulevards_in_Paris*
Sort the people alphabetically by last name
Sum up the instances of each of these
//1번
let fifteen = inventors.filter((v, idx) => v.year >= 1500 && v.year < 1600);
console.log(fifteen);
//2번
let fullName = inventors.map((v) => `${v.first} ${v.last}`);
console.log(fullName);
//3번 birthdate로 내림차순
let sortedDesc = inventors.sort((a, b) => a.year - b.year);
console.log(sortedDesc);
//4번
let totalYear = inventors.reduce((prev, curr) => { return prev + (curr.passed - curr.year)},0)
console.log(totalYear);
//5번 수명 순서로 sort
let sortedYear = inventors.sort((a, b) => {
return (a.passed - a.year) - (b.passed - b.year);
});
console.log(sortedYear);
//6번
// const category = document.querySelector('.mw-category');
// const links = category.querySelectorAll('a');
// console.log(links);
// let linksTexts = Array.from(links)
// .map((v) => v.innerHTML)
// .filter((v) => v.includes('de'));
//7번 last네임을 알파벳순으로 정렬
let sortedAlphabet = people.sort((a, b) => {
let [last, first] = a.split(", ");
let [bLast, bFirst] = b.split(", ");
return bLast - last;
});
//8번 data의 갯수 세기
let set = [...new Set(data)];
console.log(set);
let tranformation= data.reduce((obj, item) => {
if (!obj[item])
obj[item] = 0;
obj[item]++;
return obj;
}, {});
console.log(tranformation);