1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptで配列をチャンク分割

Posted at
const chunks = (a, size) =>
    Array.from(
        {length: Math.ceil(a.length / size)},
        (_, i) => a.slice(i * size, i * size + size)
    );

const array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

console.log(chunks(array, 3));
console.log(chunks(array, 2));

forとかwhileとかreduceで条件分岐するのは、なんだろう読みづらいです。個人的な感想です

参考

image.png

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?