07. Array 메서드 활용 2

Array 메서드를 배워보자 2탄

연재 WesBos Vanilla JS 30 Challenge 7/21

전체 21편 보기
  1. 01. Drum kit
  2. 02. Clock
  3. 03. CSS Variables 조절하기
  4. 04. Array 메서드 활용 1
  5. 05. Flex Panel
  6. 06. 검색 자동완성 기능 구현
  7. 07. Array 메서드 활용 2
  8. 09. 개발자 도구 활용
  9. 10. Hold Shift to check the box
  10. 11. Custom Video Player
  11. 12. Key Sequence Detection
  12. 13. 스크롤 시 이미지 슬라이드
  13. 14. 배열&객체의 복사와 참조
  14. 17. Sort Without Articles
  15. 22. Following Highlight
  16. 24. Sticky Nav
  17. 25. 이벤트 캡쳐와 버블링
  18. 26. 동적인 드롭다운 Nav
  19. 27. Click And Drag
  20. 29. CountDown Timer
  21. 30. 두더지 뚝배기
목차

💡 Array 메서드를 배워보자 2탄

배워야 할 메서드 목록 #

  1. array.some()
  2. array.every()
  3. array.find()
  4. array.findIndex()

주어진 배열 #

javascript
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() #

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

some() 메서드는 배열 내에 어떤 요소라도 주어진 판별 함수를 만족하는지 검사한다.

2. every() #

javascript
const isEvery = people.every(
  person => new Date().getFullYear - people.year >= 19,
)
console.log({ isEvery })

every() 메서드는 배열 내에 모든 요소가 주어진 판별 함수를 만족하는지 검사한다.

3. find() #

javascript
const findID = comments.find(comment => comment.id === 823423)
console.log(findID)

배열 안에서 판별함수를 만족하는 첫 번째 요소값을 반환

4. findIndex() #

javascript
const index = comments.findIndex(comment => comment.id === 823423)

const newComments = [...comments.slice(0, index), ...comments.slice(index + 1)]
console.table(newComments)

배열 안에서 판별함수를 만족하는 첫 번째 요소의 index값을 반환