frontend/JavaScript

[JS] 자바 스크립트 30개 미니 프로젝트 만들기 -7. Array Cardio : some, every, find, findIndex, slice

findTheValue 2021. 9. 6. 01:59

Array Cardio

 

1. some

const isAdult = people.some(
        (person) => new Date().getFullYear() - person.year >= 19
      );
  • python 의 any랑 같은 메서드. 하나라도 true면 true

 

2. every

const allAdults = people.every(
        (person) => new Date().getFullYear() - person.year >= 19
      );
  • python의 all. 모두 true여야 true

 

3. find, findIndex

const comment = comments.find((comment) => comment.id == 823423);
const index = comments.findIndex((comment) => comment.id === 823423);
  • 조건에 맞는 객체를 필터링해 가져옴(가장 먼저 나오는)
  • 혹은 인덱스를 반환한

 

4. slice

const newComments = [
        ...comments.slice(0, index),
        ...comments.slice(index + 1)
      ];
  • 파이썬 slice랑 똑같음. 대신 참조가 아니라 복사하기위해 앞에 ...붙여줌.