티스토리 뷰

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);
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함