KSCOLD LogoKSCOLD
Blog
Feed
Product
Admin Night
Vault
Guestbook
Info

Vault 목록

find()

연결된 노트와 함께 읽기

find()

2024년 11월 23일
  • find() 메서드(Method)는 배열(Array)에서 특정 조건을 만족하는 요소를 찾아 첫 번째 요소를 반환하는 함수(Function)이다.

  • 배열(Array)의 각 요소에 대해 콜백 함수(Callback Function)를 사용하여 원하는 조건의 요소를 찾는다.

  • 이 함수는 배열(Array)에서 특정 요소를 찾는 데 유용하다.

문법

  • find 함수는 배열(Array)의 요소를 순차적으로 순회하면서 조건에 일치하는 요소의 값을 즉시 반환한다.
  • 조건을 일치하는 경우가 없다면, undefined를 반환한다.
JavaScript

arr

  • 순회하고자 하는 배열(Array)이다.

element

  • 옵션이다.
  • 현재 배열의 요소이다.

index

  • 옵션이다.
  • 현재 배열 요소의 index이다.

thisArg

  • 옵션이다.
  • callbackFn 함수 내부에서 사용할 this 값으로 사용할 값이다.

예시

특정 요소 찾기

  • 배열(Array)에서 특정 요소를 찾을 때 사용된다.
  • 예를 들어, 특정 문자열이나 숫자를 찾을 수 있다.
JavaScript

객체 속성 기반 검색

  • 객체(Object) 배열(Array)에서 특정 속성(Property) 값에 따라 객체(Object)를 검색할 때 유용하다.
JavaScript

사용자 지정 조건에 따른 검색

  • 사용자 지정 조건 함수(Function)를 만들어 배열(Array) 요소 검색에 활용할 수 있다.
  • 만족하는 가장 첫번째 요소를 반환한다는 사실을 유의해야 한다.
JavaScript
JavaScript

Synapse Map Insight

arr.find(callback(element, index, array), thisArg)
const numbers = [1, 2, 3, 4, 5]; const target = 3; const found = numbers.find(element => element === target); console.log(found); // 출력: 3
const people = [ {name: "Alice", age: 30}, {name: "Bob", age: 25}, {name: "Charlie", age: 35} ]; const targetName = "Bob"; const person = people.find(obj => obj.name === targetName); console.log(person); // 출력: {name: "Bob", age: 25}
const products = [ {name: "Laptop", price: 1000}, {name: "Phone", price: 500}, {name: "Tablet", price: 300} ]; const customCondition = product => product.price < 600; const affordableProduct = products.find(customCondition); console.log(affordableProduct); // 출력: {name: "Phone", price: 500}
const colors = ["red", "green", "blue"]; // 콜백 함수 function findGreen(color) { return color === "green"; // 배열 요소가 "green"과 같은지 확인 }; const green = colors.find(findGreen); console.log(green); // 출력: "green"