Featurecore
Array methods
Also called map, filter, reduce, find, some, every, flat, flatMap, at, includes, Array.prototype methods, iterative methods.
The methods of Array.prototype transform, search and flatten arrays. map, filter, flat and flatMap return a new array; reduce folds the elements into one value; some methods change the array itself.
- Language
- JavaScript
Synopsis
const orders = [
{ item: "tea", qty: 2, price: 3 },
{ item: "cake", qty: 1, price: 5 },
{ item: "jam", qty: 0, price: 4 },
];
const lines = orders.filter((o) => o.qty > 0).map((o) => `${o.item} x${o.qty}`);
const total = orders.reduce((sum, o) => sum + o.qty * o.price, 0);
console.log(lines, total);
console.log(orders.find((o) => o.price > 3).item, orders.some((o) => o.qty === 0));
[ 'tea x2', 'cake x1' ] 11
cake true
| Method | Returns | Stops early |
|---|---|---|
map(fn) |
a new array of the results of fn |
no |
filter(fn) |
a new array of the elements where fn is truthy |
no |
reduce(fn, init) |
one value, folded from left to right | no |
find(fn), findIndex(fn) |
the first match, or its index | yes |
some(fn), every(fn) |
a boolean | yes |
flat(depth) |
a new array with nested arrays spread out | no |
flatMap(fn) |
map(fn) followed by flat(1) |
no |
at(i) |
the element at i; a negative i counts from the end |
no callback |
includes(v), indexOf(v) |
a boolean, or the index or -1 | no callback |
Semantics
r1. map, filter, reduce, find, some, every and flatMap are iterative methods. Their callback "is called sequentially and at most once for each element in the array".1 It gets three arguments: the element, the index and the array.2 The methods do not change the array, "but the function provided as callbackFn can."1
r2. map returns "a new array populated with the results of calling a provided function on every element".2 The callback of filter "should return a truthy value to keep the element in the resulting array, and a falsy value otherwise."3 Both copy shallowly, as all copying methods do: "The copy always happens shallowly".1 Example: pipeline.
r3. reduce runs a callback over all elements "in ascending-index order, and accumulates them into a single value."4 With an initial value, the first call gets that value and the first element. Without one, "accumulator is initialized to the first value in the array", and on an empty array the call throws a TypeError.4 Example: empty-reduce.
r4. find "returns the first element in the provided array that satisfies the provided testing function", or undefined.5 some and every stop at the first element that settles the answer. Of every, the specification says: "If such an element is found, every immediately returns false."6 Example: search.
r5. includes compares with SameValueZero, which "works like strict equality === (where -0 and +0 are considered equal), with the exception that NaN is considered equal to itself."7 indexOf uses strict equality, so "indexOf() always returns -1 when searchElement is NaN".8 The specification gives a second difference: includes "does not skip missing array elements, instead treating them as undefined."6 Example: search.
r6. at "takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array."9 An index out of range gives undefined.9
r7. flat(depth) concatenates sub-arrays into a new array, "recursively up to the specified depth", and the depth "Defaults to 1."10 It also "removes empty slots if the array being flattened is sparse."10 flatMap(fn) "is identical to map(callbackFn, thisArg) followed by flat(1)".11 Example: flatten.
r8. Some other methods change the array: push, pop, shift, unshift, splice, sort, reverse, fill and copyWithin.1 The page Copying array methods covers the copying versions of sort, reverse and splice. Example: mutate.
Examples
Searching (rules r4, r5 and r6).
const temps = [12, 17, NaN, 21, 9];
console.log(temps.find((t) => t > 15), temps.findIndex((t) => t > 15));
console.log(temps.every((t) => t > 10), temps.some((t) => t > 20));
console.log(temps.includes(NaN), temps.indexOf(NaN));
console.log(temps.at(0), temps.at(-1), temps.at(10));
17 1
false true
true -1
12 9 undefined
Flattening (rule r7). flatMap can return an empty array to drop an element.
const nested = [1, [2, [3, [4]]]];
console.log(nested.flat(), nested.flat(Infinity));
const words = ["a cup", "", "of tea"];
console.log(words.flatMap((s) => (s ? s.split(" ") : [])));
[ 1, 2, [ 3, [ 4 ] ] ] [ 1, 2, 3, 4 ]
[ 'a', 'cup', 'of', 'tea' ]
A mutating method and a copying method side by side (rule r8).
const queue = ["a", "b", "c"];
const tail = queue.slice(1);
const removed = queue.splice(0, 1);
console.log(tail, removed, queue);
[ 'b', 'c' ] [ 'a' ] [ 'b', 'c' ]
slice leaves queue as it is. splice removes the element from queue and returns the removed elements.
Pitfalls
reduce on an empty array
Without an initial value, reduce needs a first element (rule r3).
This code runs but gives a wrong answer on purpose.
const prices = [];
try {
console.log(prices.reduce((a, b) => a + b));
} catch (e) {
console.log(e.name);
}
console.log(prices.reduce((a, b) => a + b, 0));
TypeError
0
Pass the initial value, here 0, whenever the array can be empty.
A callback that takes more than one argument
map passes the index as the second argument (rule r1). MDN shows the effect with parseInt, whose second argument is the radix: for ['1', '2', '3'].map(parseInt), "the actual result is [1, NaN, NaN]".2
This code runs but gives a wrong answer on purpose.
console.log(["1", "2", "3"].map(parseInt));
console.log(["1", "2", "3"].map((s) => parseInt(s, 10)));
[ 1, NaN, NaN ]
[ 1, 2, 3 ]
Wrap the function in an arrow function that passes only the arguments it should get.
Comparison
Python's map and filter return iterators, not lists: map returns "an iterator that applies function to every item of iterable, yielding the results."12 Java has these operations on streams: "stream operations are composed into a stream pipeline", and "Streams are lazy".13 A JavaScript map or filter returns a new array.23 Go's slices package has search functions: Index "returns the index of the first occurrence of v in s, or -1 if not present", and ContainsFunc "stops as soon as a call to f returns true."14 See Collection pipeline.
References
- MDN Web Docs, JavaScript Reference, Array, Copying methods and mutating methods; Iterative methods
- MDN Web Docs, JavaScript Reference, Array.prototype.map(), Parameters; Using parseInt() with map()
- MDN Web Docs, JavaScript Reference, Array.prototype.filter(), the introduction; Parameters
- MDN Web Docs, JavaScript Reference, Array.prototype.reduce(), Parameters; Exceptions; Description
- MDN Web Docs, JavaScript Reference, Array.prototype.find(), the introduction; Return value
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), 23.1.3.6 Array.prototype.every, Note 1; 23.1.3.16 Array.prototype.includes, Note 3
- MDN Web Docs, JavaScript Reference, Array.prototype.includes(), Description
- MDN Web Docs, JavaScript Reference, Array.prototype.indexOf(), Description
- MDN Web Docs, JavaScript Reference, Array.prototype.at(), the introduction; Description
- MDN Web Docs, JavaScript Reference, Array.prototype.flat(), Parameters; Description
- MDN Web Docs, JavaScript Reference, Array.prototype.flatMap(), Description
- The Python Standard Library, Python 3.14, Built-in Functions, map(); filter()
- Java SE 25 API, Interface Stream, the class description
- Go standard library, package slices, func Index; func ContainsFunc
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag C1GVBC Edit this page History Report an error Markdown