--- id: javascript-array-iteration title: "JavaScript Array Iteration" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Array iteration", "forEach", "map", "filter", "reduce", "Array.from", "spread operator"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "array", "iteration"] raw_sources: ["https://www.w3schools.com/js/js_array_iteration.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Array Iteration]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Array iteration methods operate on every array item β€” `forEach` runs a function per element, `map`/`flatMap`/`filter` produce new arrays, `reduce`/`reduceRight` fold to a single value, and `every`/`some` test conditions β€” while `Array.from`, `keys`, `entries`, the spread `...` and rest `...` round out array traversal and construction. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`forEach()`** calls a function once for each array element. The callback receives value, index, and array; index and array are optional. [S1] - **`map()`** creates a new array by performing a function on each element, without changing the original. [S1] - **`flatMap()`** (ES2019) maps each element then flattens the result by one level. [S1] - **`filter()`** creates a new array with the elements that pass a test. [S1] - **`reduce()`** runs a function on each element to reduce the array to a single value, working left-to-right; it can take an initial value. [S1] - **`reduceRight()`** works like `reduce()` but right-to-left. [S1] - **`every()`** checks if all elements pass a test; **`some()`** checks if some (at least one) pass. [S1] - **`Array.from()`** returns an array object from any iterable, with an optional map function. [S1] - **`keys()`** returns an Array Iterator with the keys; **`entries()`** returns key/value pairs. [S1] - **`with()`** (ES2023) returns a new array with one element replaced, without mutating the original. [S1] - **The spread `...` operator** expands an iterable into individual elements; **rest `...`** collects remaining elements during destructuring. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Side-effect pass** β€” `forEach` for running an action per element. [S1] - **Transform pass** β€” `map`/`flatMap` to produce a transformed new array. [S1] - **Select pass** β€” `filter` to keep matching elements. [S1] - **Fold pass** β€” `reduce`/`reduceRight` to accumulate to one value, optionally with a seed. [S1] - **Test pass** β€” `every`/`some` for all-match / any-match booleans. [S1] - **Combine/copy** β€” spread `[...a, ...b]` to merge or `[...a]` to copy arrays. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Array forEach()** β€” calls a function (a callback function) once for each array element. The callback takes value, index, array (index and array are optional): [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction); function myFunction(value, index, array) { txt += value + "
"; } ``` With only the value parameter: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction); function myFunction(value) { txt += value + "
"; } ``` **Array map()** β€” creates a new array by performing a function on each array element. It does not execute the function for array elements without values and does not change the original array: [S1] ```javascript const numbers1 = [45, 4, 9, 16, 25]; const numbers2 = numbers1.map(myFunction); function myFunction(value, index, array) { return value * 2; } ``` With only the value parameter: [S1] ```javascript const numbers1 = [45, 4, 9, 16, 25]; const numbers2 = numbers1.map(myFunction); function myFunction(value) { return value * 2; } ``` **Array flatMap()** β€” ES2019 added `flatMap()`, which first maps all elements of an array and then creates a new array by flattening the array: [S1] ```javascript const myArr = [1, 2, 3, 4, 5, 6]; const newArr = myArr.flatMap(x => [x, x * 10]); ``` **Array filter()** β€” creates a new array with array elements that pass a test: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; const over18 = numbers.filter(myFunction); function myFunction(value, index, array) { return value > 18; } ``` With only the value parameter: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; const over18 = numbers.filter(myFunction); function myFunction(value) { return value > 18; } ``` **Array reduce()** β€” runs a function on each array element to produce (reduce it to) a single value. It works from left-to-right and does not reduce the original array: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction); function myFunction(total, value, index, array) { return total + value; } ``` With only total and value parameters: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction); function myFunction(total, value) { return total + value; } ``` With an initial value (100): [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction, 100); function myFunction(total, value) { return total + value; } ``` **Array reduceRight()** β€” runs a function on each array element to produce a single value, working from right-to-left: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduceRight(myFunction); function myFunction(total, value, index, array) { return total + value; } ``` With only total and value parameters: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduceRight(myFunction); function myFunction(total, value) { return total + value; } ``` **Array every()** β€” checks if all array values pass a test: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction); function myFunction(value, index, array) { return value > 18; } ``` With only the value parameter: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18; } ``` **Array some()** β€” checks if some array values pass a test: [S1] ```javascript const numbers = [45, 4, 9, 16, 25]; let someOver18 = numbers.some(myFunction); function myFunction(value, index, array) { return value > 18; } ``` **Array.from()** β€” returns an Array object from any object with a length property or any iterable object. Create an array from a string: [S1] ```javascript let text = "ABCDEFG"; Array.from(text); ``` With a map function: [S1] ```javascript const myNumbers = [1,2,3,4]; const myArr = Array.from(myNumbers, (x) => x * 2); ``` **Array keys()** β€” returns an Array Iterator object with the keys of an array: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; const keys = fruits.keys(); for (let x of keys) { text += x + "
"; } ``` **Array entries()** β€” returns an Array Iterator object with key/value pairs: [S1] ```javascript const fruits = ["Banana", "Orange", "Apple", "Mango"]; const f = fruits.entries(); for (let x of f) { document.getElementById("demo").innerHTML += x; } ``` **Array with()** β€” ES2023 added the `with()` method as a safe way to update elements without altering the original array: [S1] ```javascript const months = ["Januar", "Februar", "Mar", "April"]; const myMonths = months.with(2, "March"); ``` **Array Spread (...)** β€” the `...` operator expands an iterable into more elements. Combine two arrays: [S1] ```javascript const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const arr3 = [...arr1, ...arr2]; ``` Combine four arrays: [S1] ```javascript const q1 = ["Jan", "Feb", "Mar"]; const q2 = ["Apr", "May", "Jun"]; const q3 = ["Jul", "Aug", "Sep"]; const q4 = ["Oct", "Nov", "Des"]; const year = [...q1, ...q2, ...q3, ...q4]; ``` Copy an array: [S1] ```javascript const arr1 = [1, 2, 3]; const arr2 = [...arr1]; ``` Spread into Math functions: [S1] ```javascript const numbers = [23,55,21,87,56]; let minValue = Math.min(...numbers); let maxValue = Math.max(...numbers); ``` **Array Rest (...)** β€” collects remaining elements during destructuring: [S1] ```javascript let a, rest; const arr1 = [1,2,3,4,5,6,7,8]; [a, ...rest] = arr1; ``` ```javascript let a, b, rest; const arr1 = [1,2,3,4,5,6,7,8]; [a, b, ...rest] = arr1; ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” doubling numbers with `map`, filtering values over 18, summing with `reduce`, merging quarters into a year with spread, and replacing a month with `with`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Transform every element: ```javascript const numbers2 = numbers1.map(value => value * 2); ``` Keep matching elements: ```javascript const over18 = numbers.filter(value => value > 18); ``` Fold to a single value: ```javascript let sum = numbers.reduce((total, value) => total + value); ``` Merge / copy arrays: ```javascript const arr3 = [...arr1, ...arr2]; const copy = [...arr1]; ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`forEach` vs `map`** β€” `forEach` is for side effects and returns nothing useful; `map` returns a new transformed array. [S1] - **`reduce` vs `reduceRight`** β€” same folding behavior but opposite direction (left-to-right vs right-to-left). [S1] - **`every` vs `some`** β€” `every` requires all elements to pass; `some` requires at least one. [S1] - **Spread vs rest** β€” same `...` token; spread expands an iterable into elements, rest collects leftover elements during destructuring. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. Version provenance noted: `flatMap` is ES2019; `with` is ES2023. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Array Search]], [[JavaScript Array Sort]], [[JavaScript Array Const]] - **μ°Έμ‘° λ§₯락:** Referenced whenever you need to traverse, transform, filter, or reduce array data. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Array Iteration β€” https://www.w3schools.com/js/js_array_iteration.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Array Iteration" page (Astra wiki-curation, P-Reinforce v3.1 format).