[JavaScript] 07. Array 메서드 활용 2
9147 단어 JavaScriptjavascript30JavaScript
💡 Array 메서드를 배워보자 2탄
배워야 할 메서드 목록
- array.some()
- array.every()
- array.find()
- array.findIndex()
주어진 배열
const people = [
{ name: "Wes", year: 1988 },
{ name: "Kait", year: 1986 },
{ name: "Irv", year: 1970 },
{ name: "Lux", year: 2015 },
];
const comments = [
{ text: "Love this!", id: 523423 },
{ text: "Super good", id: 823423 },
{ text: "You are the best", id: 2039842 },
{ text: "Ramen is my fav food ever", id: 123523 },
{ text: "Nice Nice Nice!", id: 542328 },
];
1. some()
const isAdult = people.some(person => ((new Date()).getFullYear() - person.year >= 19);
console.log(isAult);
some() 메서드는 배열 내에 어떤 요소라도 주어진 판별 함수를 만족하는지 검사한다.
2. every()
const isEvery = people.every(
(person) => new Date().getFullYear - people.year >= 19
);
console.log({ isEvery });
every() 메서드는 배열 내에 모든 요소가 주어진 판별 함수를 만족하는지 검사한다.
3. find()
const findID = comments.find((comment) => comment.id === 823423);
console.log(findID);
배열 안에서 판별함수를 만족하는 첫 번째 요소값을 반환
4. findIndex()
const index = comments.findIndex((comment) => comment.id === 823423);
const newComments = [...comments.slice(0, index), ...comments.slice(index + 1)];
console.table(newComments);
배열 안에서 판별함수를 만족하는 첫 번째 요소의 index값을 반환
Author And Source
이 문제에 관하여([JavaScript] 07. Array 메서드 활용 2), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@amgona94/07.-Array-메서드-활용-2저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)