티스토리 뷰

자바스크립트의 배열에 대해서 공부하는 파트입니다.

 

1. Filter the list of inventors for those who were born in the 1500's (1500년대에 태어난 발명가들을 필터링하라.)

let fifteen = inventors.filter((inventor) => (inventor.year >= 1500 && inventor.year < 1600));
console.table(fifteen);

console.table로 출력하면 데이터가 테이블 형태로 나옵니다.

 

2. Give us an array of the inventors' first and last names (발명가의 이름과 성으로 구성된 배열을 제공하라.)

let fullNames = inventors.map(inventors => ({first: inventors.first, last: inventors.last}));
console.table(fullNames);

 

3. Sort the inventors by birthdate, oldest to youngest (나이를 오름차순으로 발명가를 정렬하라)

let ordered = inventors.sort((a, b) => (a.year > b.year ? 1: -1));
console.table(ordered);

 

4. How many years did all the inventors live? (모든 발명가들의 살아온 년도의 합은?)

let totalYears = inventors.reduce((total, inventor) => (total + (inventor.passed - inventor.year)), 0);
console.table(totalYears);

 

5. Sort the inventors by years lived (수명을 기준으로 정렬)

let oldest = inventors.sort((a, b) => (((a.passed - a.year) > (b.passed - b.year)) ? -1 : 1));
console.table(oldest);

 

7. Sort the people alphabetically by last name (people에서 last name을 알파벳순으로 정렬)

let alpha = people.sort((a, b) => {
  let [alastName, afirstName] = a.split(', ');
  let [blastName, bfirstName] = b.split(', ');

  return alastName > blastName ? 1 : -1;
});
console.table(alpha);

 

8. Sum up the instances of each of these (각 원소의 갯수를 구해라)

let transportation = data.reduce((obj, item) => {
  if (!obj[item]) {  // item이 없는경우 0으로 초기화
	obj[item] = 0;
  }
  obj[item]++;
  return obj;
}, {});
console.table(transportation);

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함