윤쨩이의 개발 블로그
09 [ 배열 메소드 _find() / findIndex() : 배열 요소 검색하기 ] 본문
find() : 배열에 특정 요소가 있는지 탐색한다.
findIndex() : 배열에 특정 요소가 있는지 탐색하고 그 요소가 몇번째 인덱스에 있는 지 값을 출력한다.
01) find()
구문
arr.find(callback[, thisArg])
- callback
: 배열의 각 값에 대해 실행할 함수
- thisArg
: 선택 항목. 콜백이 호출될 때 this로 사용할 객체.
예제를 통해 알아보자.
const arr = ["apple","watermelone","banana","orange","peach"];
console.log(arr.find(fruit => (fruit == "apple"))); //apple
console.log(arr.find(fruit => (fruit == "banana"))) //banana
console.log(arr.find(fruit => (fruit == "a")))
02) findIndex()
const arr = ["apple","watermelone","banana","orange","peach"];
console.log(arr.findIndex(fruit => (fruit == "apple"))); //0
console.log(arr.findIndex(fruit => (fruit == "banana"))); //2
"apple"과 "banana"의 인덱스 번호를 찾는 것을 확인할 수 있다.
참고 :
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/find
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
'자바스크립트 메소드 > 배열 메소드' 카테고리의 다른 글
11 [ 배열 메소드 _ map() : 배열 요소 추출하여 새로운 배열을 만들기 ] (0) | 2021.10.16 |
---|---|
10 [ 배열 메소드 _ filter() : 배열 요소 검색하기 (2) ] (0) | 2021.10.16 |
08 [ 배열 메소드_splice() : 배열 요소를 다른 요소로 변경 (2) ] (0) | 2021.10.16 |
07 [ 배열 메소드_slice() : 배열 요소를 다른 요소로 변경 ] (0) | 2021.10.16 |
06 [ 배열 메소드_reverse() / sort() : 배열 요소의 순서를 정렬 ] (0) | 2021.10.16 |
Comments