Skip to Content

find

find() 함수는 배열에서 주어진 조건을 만족하는 첫 번째 요소를 반환한다.
(만족하는 요소가 없으면 undefined를 반환한다)

find 형식

arr.find((value, index, array) => { // 조건을 반환 return condition })
  • value: 현재 처리 중인 요소값
  • index: 현재 요소의 인덱스
  • array: find()를 호출한 원본 배열
  • 반환값: 조건을 만족하는 첫 번째 요소 (없으면 undefined)


find 예제

const users = [ { id: 1, name: 'gori' }, { id: 2, name: 'gildong' }, { id: 3, name: 'younghee' } ] const result = users.find(user => user.id === 2) console.log(result) // { id: 2, name: 'gildong' } const notFound = users.find(user => user.id === 99) console.log(notFound) // undefined

find 예제 실행 결과

{ id: 2, name: 'gildong' } undefined


findIndex

findIndex() 함수는 배열에서 주어진 조건을 만족하는 첫 번째 요소의 인덱스를 반환한다.
(만족하는 요소가 없으면 -1을 반환한다)

findIndex 형식

arr.findIndex((value, index, array) => { return condition })
  • 반환값: 조건을 만족하는 첫 번째 요소의 인덱스 (없으면 -1)


findIndex 예제

const users = [ { id: 1, name: 'gori' }, { id: 2, name: 'gildong' }, { id: 3, name: 'younghee' } ] const index = users.findIndex(user => user.id === 2) console.log(index) // 1 const notFoundIndex = users.findIndex(user => user.id === 99) console.log(notFoundIndex) // -1

findIndex 예제 실행 결과

1 -1


find와 findIndex의 관계

find()는 요소 자체를, findIndex()는 그 요소의 인덱스를 반환한다는 점만 다르고 나머지 동작은 동일하다.

find()findIndex()
반환값조건을 만족하는 요소조건을 만족하는 요소의 인덱스
못 찾았을 때undefined-1
주 사용처객체 자체가 필요할 때배열 안에서 위치가 필요할 때 (splice 등과 함께)
const users = [ { id: 1, name: 'gori' }, { id: 2, name: 'gildong' } ] // findIndex로 위치를 찾아서 splice로 삭제하는 패턴 const index = users.findIndex(user => user.id === 1) users.splice(index, 1) console.log(users) // [{ id: 2, name: 'gildong' }]



title: some / every

some

some() 함수는 배열의 요소 중 하나라도 주어진 조건을 만족하면 true를 반환한다.

some 형식

arr.some((value, index, array) => { return condition })
  • 반환값: 조건을 만족하는 요소가 하나라도 있으면 true, 없으면 false


some 예제

const scores = [60, 70, 95, 50] const hasPassingScore = scores.some(score => score >= 90) console.log(hasPassingScore) // true const hasFailingScore = scores.some(score => score < 50) console.log(hasFailingScore) // false

some 예제 실행 결과

true false


every

every() 함수는 배열의 모든 요소가 주어진 조건을 만족하면 true를 반환한다.

every 형식

arr.every((value, index, array) => { return condition })
  • 반환값: 모든 요소가 조건을 만족하면 true, 하나라도 만족하지 않으면 false


every 예제

const scores = [60, 70, 95, 50] const allPassing = scores.every(score => score >= 50) console.log(allPassing) // true const allAbove90 = scores.every(score => score >= 90) console.log(allAbove90) // false

every 예제 실행 결과

true false


some, every, filter 비교

세 메서드 모두 조건 함수를 받지만 반환값의 형태가 다르다.

some()every()filter()
반환값booleanboolean배열
의미하나라도 만족하는가전부 만족하는가만족하는 요소들
const scores = [60, 70, 95, 50] console.log(scores.some(s => s >= 90)) // true (하나라도 90 이상 있음) console.log(scores.every(s => s >= 90)) // false (전부 90 이상은 아님) console.log(scores.filter(s => s >= 90)) // [95] (90 이상인 것만 모음)



title: includes / indexOf

includes

includes() 함수는 배열(또는 문자열)에 특정 값이 포함되어 있는지를 boolean으로 반환한다.

includes 형식

arr.includes(searchValue, fromIndex)
  • searchValue: 찾을 값
  • fromIndex: 검색을 시작할 인덱스 (생략 시 0부터)
  • 반환값: 포함 여부 (true / false)


includes 예제

const names = ['gori', 'gildong', 'younghee'] console.log(names.includes('gori')) // true console.log(names.includes('chulsu')) // false // 문자열에도 사용 가능 const str = 'hello gori' console.log(str.includes('gori')) // true

includes 예제 실행 결과

true false true


indexOf

indexOf() 함수는 배열(또는 문자열)에서 특정 값이 처음 나타나는 인덱스를 반환한다.
(없으면 -1을 반환한다)

indexOf 형식

arr.indexOf(searchValue, fromIndex)
  • searchValue: 찾을 값
  • fromIndex: 검색을 시작할 인덱스 (생략 시 0부터)
  • 반환값: 처음 발견된 인덱스 (없으면 -1)


indexOf 예제

const names = ['gori', 'gildong', 'younghee'] console.log(names.indexOf('gildong')) // 1 console.log(names.indexOf('chulsu')) // -1 const str = 'hello gori' console.log(str.indexOf('gori')) // 6

indexOf 예제 실행 결과

1 -1 6


includes와 indexOf의 관계

includes()는 “있는지 없는지”만 알고 싶을 때, indexOf()는 “어디에 있는지”까지 알아야 할 때 사용한다.

includes()indexOf()
반환값boolean인덱스 (없으면 -1)
주 사용처포함 여부만 확인위치까지 필요할 때
NaN 찾기가능 (true 반환)불가능 (항상 -1)
const arr = [1, NaN, 3] console.log(arr.includes(NaN)) // true console.log(arr.indexOf(NaN)) // -1 (indexOf는 NaN을 찾지 못함)

단순히 포함 여부만 확인한다면 includes()가 의도를 더 명확하게 드러내고, NaN도 정상적으로 찾을 수 있어서 더 안전하다.



마지막 수정