LoginSignup
6
6

More than 5 years have passed since last update.

Underscore.js で配列の分割を実装する

Last updated at Posted at 2015-02-13

PHPでいうところの array_chunkUnderscore.js で実装する。

javascript
_.mixin({
    chunk: function(array, size) {
        return _.chain(array).groupBy(function(element, index) {
            return Math.floor(index / size);
        }).toArray().value();
    }
});

_.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 4);

/*
[ [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9]
]
*/
6
6
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
6
6