Lodash _.chunk() Method Example

The _.chunk() function creates an array of elements split into groups the length of the specified size.

Learn Lodash JS at Lodash JS Tutorial with Examples.

Lodash _.chunk() Method Example

<!DOCTYPE html>
<html>
<head>
    <title>Lodash Tutorial</title>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
    <script type="text/javascript">
  // Lodash chunking array
        nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];

        let c1 = _.chunk(nums, 2);
        console.log(c1);

        let c2 = _.chunk(nums, 3);
        console.log(c2);
    </script>
</head>
<body></body>
</html>
The above HTML prints below output on the console:
[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9 ] ]
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

Reference



Comments