9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
164 lines
7.6 KiB
Markdown
164 lines
7.6 KiB
Markdown
---
|
||
id: javascript-array-search
|
||
title: "JavaScript Array Search"
|
||
category: "Frontend"
|
||
status: "draft"
|
||
verification_status: "conceptual"
|
||
canonical_id: ""
|
||
aliases: ["Array search", "indexOf", "find array", "findIndex", "includes array", "findLast"]
|
||
duplicate_of: ""
|
||
source_trust_level: "B"
|
||
confidence_score: 0.89
|
||
created_at: 2026-06-23
|
||
updated_at: 2026-06-23
|
||
review_reason: ""
|
||
merge_history: []
|
||
tags: ["javascript", "js", "web", "frontend", "w3schools", "array", "search"]
|
||
raw_sources: ["https://www.w3schools.com/js/js_array_search.asp"]
|
||
applied_in: []
|
||
github_commit: ""
|
||
---
|
||
|
||
# [[JavaScript Array Search]]
|
||
|
||
## 🎯 한 줄 통찰 (One-line insight)
|
||
JavaScript offers a family of array search methods — position-based (`indexOf`, `lastIndexOf`, `includes`) and predicate-based (`find`, `findIndex`, `findLast`, `findLastIndex`) — that let you locate either a value's index or the first/last element matching a test. [S1]
|
||
|
||
## 🧠 핵심 개념 (Core concepts)
|
||
- **`indexOf()`** searches an array for a value and returns its first matching position (0-indexed), or `-1` if not found. [S1]
|
||
- **`lastIndexOf()`** works like `indexOf()` but returns the position of the *last* occurrence. [S1]
|
||
- **`includes()`** (ES2016) checks whether an element exists in an array and, unlike `indexOf()`, can correctly detect `NaN` values. [S1]
|
||
- **`find()`** (ES6) returns the value of the first element that passes a test function. [S1]
|
||
- **`findIndex()`** returns the index of the first element that passes a test function. [S1]
|
||
- **`findLast()`** (ES2023) returns the value of the last element that passes a test, searching from the end. [S1]
|
||
- **`findLastIndex()`** (ES2023) returns the index of the last element that passes a test. [S1]
|
||
- **The callback for `find`/`findIndex`** receives three arguments: the value, the index, and the array itself. [S1]
|
||
|
||
## 🧩 추출된 패턴 (Extracted patterns)
|
||
- **Position lookup pattern** — call `indexOf`/`lastIndexOf` and add `+ 1` to convert a 0-based index into a human-readable position. [S1]
|
||
- **Existence check pattern** — prefer `includes()` over `indexOf() !== -1` when you only need a boolean and need `NaN` to be detectable. [S1]
|
||
- **Predicate search pattern** — pass a test function (named or arrow) to `find`/`findIndex`/`findLast`/`findLastIndex` to locate by condition rather than by exact value. [S1]
|
||
|
||
## 📖 세부 내용 (Details)
|
||
**indexOf()** — Searches an array for a value and returns its position. The first item is at position 0, the second at position 1, and so on. It returns `-1` if the item is not found. If the item is present more than once, it returns the position of the first occurrence. [S1]
|
||
|
||
Syntax:
|
||
```javascript
|
||
array.indexOf(item, start)
|
||
```
|
||
|
||
Search an array for the item "Apple":
|
||
```javascript
|
||
const fruits = ["Apple", "Orange", "Apple", "Mango"];
|
||
let position = fruits.indexOf("Apple") + 1;
|
||
```
|
||
|
||
**lastIndexOf()** — `Array.lastIndexOf()` is the same as `Array.indexOf()`, but returns the position of the last occurrence of the specified element. [S1]
|
||
|
||
Syntax:
|
||
```javascript
|
||
array.lastIndexOf(item, start)
|
||
```
|
||
|
||
Search an array for the item "Apple":
|
||
```javascript
|
||
const fruits = ["Apple", "Orange", "Apple", "Mango"];
|
||
let position = fruits.lastIndexOf("Apple") + 1;
|
||
```
|
||
|
||
**includes()** — ECMAScript 2016 introduced `Array.includes()` to arrays. This allows you to check if an element is present in an array (including `NaN`, unlike `indexOf`). [S1]
|
||
```javascript
|
||
const fruits = ["Banana", "Orange", "Apple", "Mango"];
|
||
fruits.includes("Mango"); // is true
|
||
```
|
||
|
||
Syntax:
|
||
```javascript
|
||
array.includes(search-item)
|
||
```
|
||
|
||
**find()** — The `find()` method returns the value of the first array element that passes a test function. This example finds (returns the value of) the first element that is larger than 18: [S1]
|
||
```javascript
|
||
const numbers = [4, 9, 16, 25, 29];
|
||
let first = numbers.find(myFunction);
|
||
|
||
function myFunction(value, index, array) {
|
||
return value > 18;
|
||
}
|
||
```
|
||
The function takes 3 arguments: the item value, the item index, and the array itself. [S1]
|
||
|
||
**findIndex()** — The `findIndex()` method returns the index of the first array element that passes a test function. This example finds the index of the first element that is larger than 18: [S1]
|
||
```javascript
|
||
const numbers = [4, 9, 16, 25, 29];
|
||
let first = numbers.findIndex(myFunction);
|
||
|
||
function myFunction(value, index, array) {
|
||
return value > 18;
|
||
}
|
||
```
|
||
|
||
**findLast()** — ES2023 added the `findLast()` method that will start from the end of an array and return the value of the first element that satisfies a condition. Finding the last temperature above 40: [S1]
|
||
```javascript
|
||
const temp = [27, 28, 30, 40, 42, 35, 30];
|
||
let high = temp.findLast(x => x > 40);
|
||
```
|
||
|
||
**findLastIndex()** — The `findLastIndex()` method finds the index of the last element that satisfies a condition. Finding the index of the last temperature above 40: [S1]
|
||
```javascript
|
||
const temp = [27, 28, 30, 40, 42, 35, 30];
|
||
let pos = temp.findLastIndex(x => x > 40);
|
||
```
|
||
|
||
**Browser support** — The page documents browser support across Chrome, Edge, Firefox, Safari, and Opera, with most of these features fully supported since their ES2016–ES2023 introductions. [S1]
|
||
|
||
## 🛠️ 적용 사례 (Applied in summary)
|
||
The page's own snippets are the canonical applied examples — locating "Apple" in a fruits array, finding the first number over 18, and finding the last temperature above 40. No external project/commit applications found in the source.
|
||
|
||
## 💻 코드 패턴 (Code patterns)
|
||
Find a value's position:
|
||
```javascript
|
||
const fruits = ["Apple", "Orange", "Apple", "Mango"];
|
||
let position = fruits.indexOf("Apple") + 1;
|
||
```
|
||
Check existence:
|
||
```javascript
|
||
const fruits = ["Banana", "Orange", "Apple", "Mango"];
|
||
fruits.includes("Mango"); // is true
|
||
```
|
||
Find first element by condition:
|
||
```javascript
|
||
const numbers = [4, 9, 16, 25, 29];
|
||
let first = numbers.find((value, index, array) => value > 18);
|
||
```
|
||
Find last element by condition (ES2023):
|
||
```javascript
|
||
const temp = [27, 28, 30, 40, 42, 35, 30];
|
||
let high = temp.findLast(x => x > 40);
|
||
```
|
||
|
||
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
||
- **`indexOf` vs `includes`** — use `indexOf`/`lastIndexOf` when you need the position; use `includes` when you need a boolean and especially when `NaN` must be detectable (which `indexOf` cannot do). [S1]
|
||
- **`find`/`findIndex` vs `findLast`/`findLastIndex`** — the former search from the start of the array; the latter (ES2023) search from the end. Choose value-returning (`find`/`findLast`) vs index-returning (`findIndex`/`findLastIndex`) based on what you need. [S1]
|
||
|
||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||
No contradictions found in the source. The source notes version provenance: `includes` is ES2016; `find`/`findIndex` are ES6; `findLast`/`findLastIndex` are ES2023. [S1]
|
||
|
||
## ✅ 검증 상태 및 신뢰도
|
||
- **상태:** draft
|
||
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||
- **신뢰 점수:** 0.89
|
||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||
|
||
## 🔗 지식 그래프 (Knowledge Graph)
|
||
- **상위/루트:** [[JavaScript Tutorial]]
|
||
- **관련 개념:** [[JavaScript Array Sort]], [[JavaScript Array Iteration]], [[JavaScript Arrays]]
|
||
- **참조 맥락:** Referenced whenever you need to locate elements within an array by value or by predicate.
|
||
|
||
## 📚 출처 (Sources)
|
||
- [S1] W3Schools — JavaScript Array Search — https://www.w3schools.com/js/js_array_search.asp
|
||
|
||
## 📝 변경 이력 (Change history)
|
||
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Array Search" page (Astra wiki-curation, P-Reinforce v3.1 format).
|