1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

配列の処理を途中で止めたい場合

Last updated at Posted at 2025-05-09

前提

 配列と、

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

 重い処理の関数があって、

const slowFunction = x => {
    console.log(`slowFunction called with ${x}`);
    return x;
}

 これを配列の先頭から適用して、その結果を判定するという繰り返し処理があったとして、

const isOK = x => {
    return x > 5;
}

 判定関数で有効なものをひとつでも取得できればそれでいい場合、途中で処理を打ち切りたい時、どう書くか、という話です。
 Array.prototype.reduceとかを使ってしまうと最後まで走査してしまいますし、Array.prototype.someは途中で停止できますが処理後の値を取得することができないです。

結論

 Iterator Helper が素敵。

今までのコード

 おそらく、ループを使ってこんな感じで書くか、

let result = null;
for (let i = 0; !isOK(result) && i < arr.length; i++, result = slowFunction(arr[i])) { } // breakでもよい
console.log(result);

 Array.prototype.someなどの関数の副作用を使って書くか、

let result = null;
arr.some(x => { // findでもよい
    result = slowFunction(x);
    return isOK(result);
});
console.log(result);

 という感じだったかと思います。
 いずれにせよ、テンポラリ変数をletで準備しなくてはいけない、というのはちょっとダサい感じがします。

まさかの時の Iterator Helper

 これからはこう書けます。

arr.values().map(slowFunction).filter(isOK).take(1).toArray()[0]

 遅延評価(?)してくれるので、必要のない関数呼び出しはしないのが素敵。

See the Pen Untitled by lhankor_mhy (@lhankor_mhy) on CodePen.

 他の言語ではわりと当たり前だと思うのですが、JavaScriptは2025年になってようやく実装がそろってきました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?