JavaScript Array Methods You Are Probably Underusing in 2026
Beyond map, filter, and reduce — discover the array methods that will make your code cleaner, faster, and more expressive. Includes groupBy, findLast, toSorted, and more.
Every JavaScript developer knows map, filter, and reduce. But the language has evolved significantly, and there are powerful array methods that most developers either don't know about or habitually skip. Let's fix that.
Object.groupBy() — Finally, Native Grouping
For years, we reached for Lodash's _.groupBy or wrote manual reduce loops. Object.groupBy() (ES2024, now baseline in all major browsers and Node 22+) does it natively:
interface BlogPost {
title: string;
category: string;
status: "draft" | "published";
}
const posts: BlogPost[] = [
{ title: "TypeScript Tips", category: "typescript", status: "published" },
{ title: "React Patterns", category: "react", status: "draft" },
{ title: "TS Generics", category: "typescript", status: "published" },
{ title: "Next.js SSR", category: "react", status: "published" },
];
// Group by category
const byCategory = Object.groupBy(posts, (post) => post.category);
// { typescript: [...], react: [...] }
// Group by status — perfect for dashboard views
const byStatus = Object.groupBy(posts, (post) => post.status);
// { draft: [1 post], published: [3 posts] }
// Also: Map.groupBy() when you need non-string keys
const byLength = Map.groupBy(posts, (post) =>
post.title.length > 15 ? "long" : "short"
);
toSorted(), toReversed(), toSpliced() — Immutable Operations
React developers know the pain of accidentally mutating state arrays. The new immutable array methods return new arrays instead of modifying in place:
const numbers = [3, 1, 4, 1, 5, 9];
// Old way: clone first, then sort (easy to forget the clone)
const sortedOld = [...numbers].sort((a, b) => a - b);
// New way: returns a new sorted array, original untouched
const sorted = numbers.toSorted((a, b) => a - b);
// [1, 1, 3, 4, 5, 9] — numbers is still [3, 1, 4, 1, 5, 9]
const reversed = numbers.toReversed();
// [9, 5, 1, 4, 1, 3]
// toSpliced replaces splice() immutably
const withReplacement = numbers.toSpliced(2, 1, 99);
// [3, 1, 99, 1, 5, 9]
// with() replaces a single index immutably
const updated = numbers.with(0, 100);
// [100, 1, 4, 1, 5, 9]
// Perfect for React state updates
const [items, setItems] = useState(["a", "b", "c"]);
setItems((prev) => prev.toSorted());
setItems((prev) => prev.with(1, "updated"));
findLast() and findLastIndex()
When you need the last matching element, don't reverse the array first:
const logs = [
{ level: "info", message: "Started" },
{ level: "error", message: "Connection failed" },
{ level: "info", message: "Retrying" },
{ level: "error", message: "Timeout" },
];
// Last error in the log
const lastError = logs.findLast((log) => log.level === "error");
// { level: "error", message: "Timeout" }
const lastErrorIndex = logs.findLastIndex((log) => log.level === "error");
// 3
Array.from() with Map Function
Most developers use Array.from() to convert iterables, but forget it takes a second argument — a map function that's more memory-efficient than chaining .map():
// Generate an array of 10 placeholder items
const placeholders = Array.from({ length: 10 }, (_, i) => ({
id: i,
title: `Loading item ${i + 1}...`,
skeleton: true,
}));
// Convert a Set and transform in one pass (no intermediate array)
const uniqueUpperNames = Array.from(
new Set(["alice", "bob", "alice", "carol"]),
(name) => name.toUpperCase()
);
// ["ALICE", "BOB", "CAROL"]
Iterator.from() — ES2026 Preview
The Iterator Helpers proposal (Stage 4, shipping in V8) brings lazy evaluation to JavaScript. Instead of creating intermediate arrays, you can chain operations on iterators:
// Process large datasets lazily — no intermediate arrays
const results = Iterator.from(hugeArray)
.filter((item) => item.active)
.map((item) => item.name)
.take(10)
.toArray();
Takeaways
- Use
Object.groupBy()instead of Lodash or manual reduce for grouping - Prefer
toSorted(),toReversed(),toSpliced(), andwith()for immutable array operations in React - Use
findLast()when you need the last match — don't reverse first - Remember
Array.from()takes a map function as its second argument - Keep an eye on Iterator Helpers for lazy array processing
Modern JavaScript gives you the tools to write cleaner, more expressive code without third-party libraries. Start using these methods today — your bundle size and your code reviewers will thank you.
Admin
Bình luận (0)
Đăng nhập để bình luận
Chưa có bình luận nào. Hãy là người đầu tiên!