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

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay