DEV Community

Viktor Savchenko
Viktor Savchenko

Posted on

Разбор chunk() в lodash

Функция chunk позволяет разбить массив на подмассивы заданного размера.

Синтаксис

chunk(array, [size=1])

array - массив, к которому необходимо применить трансформацию
size ( необязательный, по-умолчанию равен 1 ) - размер получаемых подмассивов.

Пример кода №1

const lodash = require("lodash");

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

const chunked_arr = lodash.chunk(arr);

console.log(chunked_arr);
Enter fullscreen mode Exit fullscreen mode

Результат
Скриншот результата кода

Пример кода №2

const lodash = require("lodash");

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

const chunked_arr = lodash.chunk(arr, 3);

console.log(chunked_arr);

Enter fullscreen mode Exit fullscreen mode

Результат

Скриншот результата кода

Пример кода №3

const lodash = require("lodash");

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

const chunked_arr = lodash.chunk(arr, 2);

console.log(chunked_arr);

Enter fullscreen mode Exit fullscreen mode

Результат
Скриншот результата кода

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free