2020-01-13
정리
: Javascript30을 공부한 내용 기록
some 메서드는 배열 안의 어떤 요소라도 주어진 판별 함수를 통과하는지 테스트한다.
const isAdult = people.some(
(person) => new Date().getFullYear - person.year >= 19
);
every 메서드는 배열 안의 모든 요소가 주어진 판별 함수를 통과하는지 테스트한다.
const isEveryAdult = people.every(
(person) => new Date().getFullYear - person.year >= 19
);
find 메서드는 주어진 판별 함수를 만족하는 첫 번째 요소의 값을 반환한다. 그런 요소가 없다면 undefined를 반환한다.
const target = comments.find((comment) => comment.id === 823423);
findIndex 메서드는 주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환한다. 만족하는 요소가 없으면 -1을 반환한다.
const index = comments.findIndex((comment) => comment.id === 823423);
slice 메서드는 어떤 배열의 begin부터 end까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환한다. 원본 배열은 바뀌지 않는다.
// immutable
const newComments = [...comments.slice(0, index), ...comments.slice(index + 1)];
🏆
slice
와 유사한splice
를 알아보자.
splice 메서드는 배열의 기존 요소를 삭제 또는 교체하거나 새 요소를 추가하여 배열의 내용을 변경한다.
// mutable
comments.splice(index, 1);
👋