iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔰

Beyond .filter(): Essential JavaScript Array Methods You Should Know

に公開
2025/06/11
3件

Introduction

It's about the time of year when training for newly hired graduate engineers ends and they begin their department assignments, so I'd like to share an article aimed primarily at JavaScript beginners.

Not just beginners, but I often see people even at an intermediate level who end up being a "person who uses .filter() for everything." I hope you'll take this opportunity to learn that there are often better ways to do things.

Useful Built-in Methods

JavaScript arrays (data structures that hold multiple values in a list format, like ["apple", "banana", "cherry"]) come equipped with standard methods such as .sort() and .map().

These are powerful tools for writing array-related logic that are undoubtedly more efficient and easier to understand than writing your own for loops. Many other useful standard functions are available besides the ones mentioned here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods

Since these are what we call "higher-order functions"—functions that take other functions as arguments—it might take a little time to get used to how to use them, but they are essential techniques to master if you use JavaScript.

.filter()

.filter() is a built-in method that extracts only the elements from an array that meet a specific condition.

filter-example.ts
const numbers = [1, 2, 3, 4, 5];
const oddNumbers = numbers.filter(value => value % 2 === 1);
// oddNumbers: [1, 3, 5]

It is a very versatile method, so it's definitely worth knowing.

However, .filter() isn't the only useful method!

.find()

.find() returns the first element in the array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

find-example.ts
const users = [
  {id: '0000', name: 'Taro'},
  {id: '0001', name: 'Jiro'},
  {id: '0002', name: 'Saburo'}
];
const user = users.find(user => user.id === '0000');
// user: {id: '0000', name: 'Taro'}

It is similar to .filter(), but it terminates processing the moment a matching element is found, so there is no wasted effort. While you can achieve the same result with .filter(), it wouldn't stop after finding 'Taro' and would continue to perform filtering on 'Jiro' and 'Saburo' as well.

Are you writing your code like this?

find-using-filter-example.ts
// Bad example: inspects all elements
const user = users.filter(user => user.id === '0000')[0];

// Good example: terminates processing as soon as the first matching element is found
const user = users.find(user => user.id === '0000');

Using .find() not only improves performance but also makes the purpose of "finding one item" clear, improving readability compared to .filter()[0].

.some()

.some() determines whether at least one element in the array satisfies the condition. It returns true if at least one matching element is found, and false if none are found.

some-example.ts
const numbers = [1, 2, 3, 4, 5];
const hasEvenNumber = numbers.some(num => num % 2 === 0);
// hasEvenNumber: true (since 2 and 4 are even)

const users = [
  {name: 'Taro', age: 27},
  {name: 'Jiro', age: 25},
  {name: 'Saburo', age: 22}
];
const hasAdult = users.some(user => user.age >= 18);
// hasAdult: true

It is efficient because it terminates processing the moment a matching element is found.

Are you writing your code like this?

some-using-filter-example.ts
// Bad example: inspects all elements
const hasAdult = users.filter(user => user.age >= 18).length > 0;

// Good example: terminates as soon as a matching element is found
const hasAdult = users.some(user => user.age >= 18);

.every()

.every() determines whether all elements in the array satisfy the condition. It returns true if all elements satisfy the condition, and false if at least one does not.

every-example.ts
const numbers = [2, 4, 6, 8];
const allEven = numbers.every(num => num % 2 === 0);
// allEven: true (since all are even)

const users = [
  {name: 'Taro', age: 22},
  {name: 'Jiro', age: 20},
  {name: 'Saburo', age: 17}
];
const allAdults = users.every(user => user.age >= 18);
// allAdults: false (since Saburo is 17 and a minor)

This is also efficient because it terminates processing as soon as an element that doesn't satisfy the condition is found.

Are you writing your code like this?

every-using-filter-example.ts
// Bad example: filters all elements and checks length
const allAdults = users.filter(user => user.age >= 18).length === users.length;

// Good example: terminates as soon as a non-matching element is found
const allAdults = users.every(user => user.age >= 18);

Summary

Method Purpose Return Value Early Termination
.filter() Extract all elements matching a condition Array None
.find() Retrieve the first element matching a condition Element or undefined Yes
.some() Determine if at least one element matches a condition boolean Yes
.every() Determine if all elements match a condition boolean Yes

Conclusion

In array operations, choosing the appropriate method based on your objective can improve both code readability and performance.

Especially when dealing with large arrays, you can reduce unnecessary processing by appropriately using .find(), .some(), and .every(), which support early termination.

It might feel unfamiliar at first, but once you master these methods, you'll be able to write more beautiful and efficient JavaScript code. Please try to use them actively!

Writing Code Even More Efficiently

By introducing utility libraries that provide general-purpose functions not found in standard methods, such as unique() or groupBy(), you can write even more readable code.
I recommend Remeda, which is type-safe, small in size, and computationally efficient!

https://zenn.dev/kiyoshiro9446/articles/utility-remeda

Discussion

junerjuner

配列として処理するのではなくイテレータとして処理させる為の .values() とかも便利ですよね。(配列の生成回数が減らせますので

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/values

イテレータだと標準メソッドとして 列挙する件数を制限する .take() や スキップする .drop() 配列を生成する .toArray() なども含まれています

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Iterator

2