LoginSignup
0
0

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

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