javascript/javascript30

07 - Array Cardio Day 2 (Javascript 30)

oraclian 2019. 10. 19. 21:57

javascript의 array에 대해서 배우는 시간 2편입니다.

여기서는 some, every, find, findindex에 대해서 공부를하게 됩니다.

 

1. is at least one person 19 or older? (한 사람 이상이 19세 이상인가?)

const isAdult = people.some(person => ((new Date().getFullYear()) - person.year) >= 19);
console.log(isAdult);


2. is everyone 19 or older? (모두가 19세 이상인가?)

const allAdult = people.every(person => ((new Date().getFullYear()) - person.year) >= 19);
console.log(allAdult);

 

3. Find is like filter, but instead returns just the one you are looking for find the comment with the ID of 823423

(댓글 중 id가 823423인것 찾기)

const comment = comments.find(comment => comment.id === 823423);
console.table(comment);

 

4. Find the comment with this ID delete the comment with the ID of 823423 (댓글 중 id가 823423인것 삭제)

const index = comments.findIndex(comment => comment.id === 823423);
const newComments = [
  ...comments.slice(0, index),
  ...comments.slice(index + 1)
];

console.table(newComments);