JavaScript Array Methods
JavaScript array methods cheat sheet with map, filter, reduce, find, sort, and practical code examples for data transformation.

PDF · 2 pages
JavaScript Array Methods PDF cheat sheet
A two-page JavaScript Array Methods reference for printing or offline use.
Other JavaScript Sheets
Transformation Methods
Transform arrays into new arrays with modified data
Create a new array by transforming each element
// Create new array by transforming each element
const doubled = [1, 2, 3].map(x => x * 2);
// [2, 4, 6]
const users = [{name: 'John', age: 30}];
const names = users.map(u => u.name);
// ['John']
// With index
const indexed = ['a', 'b'].map((val, i) => `${i}: ${val}`);
// ['0: a', '1: b']Create a new array with elements that pass a test
// Keep elements that pass the test
const evens = [1, 2, 3, 4].filter(x => x % 2 === 0);
// [2, 4]
const adults = users.filter(u => u.age >= 18);
// Remove falsy values
const truthy = [0, 1, '', 'hello', null].filter(Boolean);
// [1, 'hello']Reduce array to single value using accumulator
// Sum all numbers
const sum = [1, 2, 3].reduce((acc, x) => acc + x, 0);
// 6
// Group by property
const grouped = items.reduce((acc, item) => {
acc[item.category] = acc[item.category] || [];
acc[item.category].push(item);
return acc;
}, {});Searching & Testing
Find elements and test array conditions
Find first element matching condition or its index
// Find first matching element
const found = [1, 2, 3].find(x => x > 1);
// 2
// Find index of first match
const index = [1, 2, 3].findIndex(x => x > 1);
// 1
// Find in objects
const user = users.find(u => u.id === 123);
const userIndex = users.findIndex(u => u.id === 123);Test if any/all elements pass a condition
// Test if ANY element passes
const hasEven = [1, 2, 3].some(x => x % 2 === 0);
// true
// Test if ALL elements pass
const allPositive = [1, 2, 3].every(x => x > 0);
// true
// Check for empty array
const isEmpty = array.length === 0 || array.every(x => !x);Check if array contains a value or find its position
// Check if array contains value
[1, 2, 3].includes(2); // true
['a', 'b'].includes('c'); // false
// Find index of value
[1, 2, 3].indexOf(2); // 1
['a', 'b'].indexOf('c'); // -1
// Starting from index
arr.includes(value, fromIndex);
arr.indexOf(value, fromIndex);Array Manipulation
Add, remove, and rearrange array elements
Methods to add and remove array elements
// Add/remove from end
arr.push(4, 5); // Returns new length
arr.pop(); // Returns removed element
// Add/remove from start
arr.unshift(0); // Returns new length
arr.shift(); // Returns removed element
// Add/remove anywhere
arr.splice(1, 2, 'a', 'b'); // At index 1, remove 2, add 'a', 'b'Extract portions and combine arrays without mutation
// Copy portion of array
const portion = arr.slice(1, 3); // Items at index 1, 2
const last3 = arr.slice(-3); // Last 3 items
const copy = arr.slice(); // Full copy
// Combine arrays
const combined = arr1.concat(arr2, arr3);
const combined = [...arr1, ...arr2]; // Modern waySort and reverse array elements in place
// Sort (mutates array!)
arr.sort(); // Alphabetical
arr.sort((a, b) => a - b); // Numeric ascending
arr.sort((a, b) => b - a); // Numeric descending
// Reverse (mutates array!)
arr.reverse();
// Non-mutating versions
const sorted = [...arr].sort();
const reversed = [...arr].reverse();Iteration Methods
Loop through arrays and process elements
Execute function for each element (side effects only)
// Execute function for each element (no return)
[1, 2, 3].forEach(x => console.log(x));
// With index and array
arr.forEach((val, index, array) => {
console.log(`${index}: ${val}`);
});
// Can't break early (use for...of instead)Convert array to string and vice versa
// Array to string
[1, 2, 3].join(); // '1,2,3'
[1, 2, 3].join(' - '); // '1 - 2 - 3'
[1, 2, 3].join(''); // '123'
// String to array
'1,2,3'.split(','); // ['1', '2', '3']
'hello'.split(''); // ['h', 'e', 'l', 'l', 'o']
Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']Flatten nested arrays and map-then-flatten
// Flatten nested arrays
[1, [2, 3]].flat(); // [1, 2, 3]
[1, [2, [3]]].flat(2); // [1, 2, 3] (depth 2)
// Map and flatten
[1, 2].flatMap(x => [x, x * 2]); // [1, 2, 2, 4]
// Remove empty slots
[1, , 3].flat(); // [1, 3]Array Creation & Conversion
Create new arrays and convert between types
Create arrays from array-like objects or iterables
// Create array from iterable
Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']
Array.from(new Set([1, 2])); // [1, 2]
// With mapping function
Array.from({length: 5}, (_, i) => i); // [0, 1, 2, 3, 4]
// Create array from arguments
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(7); // [7] (not empty array of length 7)Fill array with values or copy elements within
// Fill array with value
[1, 2, 3].fill(0); // [0, 0, 0]
[1, 2, 3].fill(0, 1); // [1, 0, 0]
[1, 2, 3].fill(0, 1, 2); // [1, 0, 3]
// Copy within array
[1, 2, 3, 4, 5].copyWithin(0, 3); // [4, 5, 3, 4, 5]Modern Array Methods (ES2023+)
Immutable variants, findLast, fromAsync, and grouping (ES2023+)
Non-mutating versions of sort/reverse/splice and indexed update
const nums = [3, 1, 2]
const sorted = nums.toSorted() // [1, 2, 3] — nums untouched
const reversed = nums.toReversed() // [2, 1, 3] — nums untouched
const removed = nums.toSpliced(0, 1) // [1, 2] — nums untouched
const updated = nums.with(0, 99) // [99, 1, 2] — nums untouched
console.log(nums) // [3, 1, 2] — original preservedSearch from the end — perfect for "most recent matching"
const events = [
{ type: 'login', at: 1 },
{ type: 'click', at: 2 },
{ type: 'login', at: 3 },
{ type: 'logout', at: 4 },
]
const lastLogin = events.findLast(e => e.type === 'login')
// { type: 'login', at: 3 }
const lastLoginIdx = events.findLastIndex(e => e.type === 'login')
// 2Build arrays from async iterables, group by key (Object.groupBy / Map.groupBy)
// Array.fromAsync — collect an async iterable into an array
async function* lines(file) {
for await (const chunk of file.stream()) yield chunk
}
const allLines = await Array.fromAsync(lines(myFile))
// Object.groupBy — group items by a derived key (ES2024)
const items = [
{ name: 'apple', type: 'fruit' },
{ name: 'carrot', type: 'veg' },
{ name: 'pear', type: 'fruit' },
]
const byType = Object.groupBy(items, x => x.type)
// { fruit: [{...apple}, {...pear}], veg: [{...carrot}] }
// Map.groupBy — same idea, but the key can be any value (incl. objects)
const byParent = Map.groupBy(rows, r => r.parent)