[Algorithm] Chunk Array
Array ALGORITHM chunk
2023-09-14 08:59:14 时间
// --- Directions
// Given an array and chunk size, divide the array into many subarrays
// where each subarray is of length size
// --- Examples
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
function chunk(array, size) { let result = []; let temp = []; const len = array.length; for (let i = 0; i < len; i++) { let mod = i % size; if (mod === 0) { if (temp.length !== 0) { result = [...result, temp]; } temp = []; } temp[mod] = array[i]; } result = [...result, temp]; return result; }
way 2:
function chunk(array, size) { let chunked = []; let index = 0; while (index < array.length) { chunked.push(array.slice(index, index + size)); index += size; } return chunked; }
test:
const chunk = require("./index"); test("function chunk exists", () => { expect(typeof chunk).toEqual("function"); }); test("chunk divides an array of 10 elements with chunk size 2", () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const chunked = chunk(arr, 2); expect(chunked).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]); }); test("chunk divides an array of 3 elements with chunk size 1", () => { const arr = [1, 2, 3]; const chunked = chunk(arr, 1); expect(chunked).toEqual([[1], [2], [3]]); }); test("chunk divides an array of 5 elements with chunk size 3", () => { const arr = [1, 2, 3, 4, 5]; const chunked = chunk(arr, 3); expect(chunked).toEqual([[1, 2, 3], [4, 5]]); }); test("chunk divides an array of 13 elements with chunk size 5", () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; const chunked = chunk(arr, 5); expect(chunked).toEqual([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]); });
相关文章
- PHP Object 转 Array,Json 转 Array
- (LeetCode 189)Rotate Array
- [Algorithm] Print 2-D array in spiral order
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
- [Algorithm] Array production problem
- [Algorithm] Heap data structure and heap sort algorithm
- [Python] Array Attributes of Numpy lib
- [ES2016] Check if an array contains an item using Array.prototype.includes
- [Javascript] Array methods in depth - indexOf
- [ES6] Converting an array-like object into an Array with Array.from()
- [Javascript] Filter out Duplicates from Flat JavaScript Array with array.filter / reduce / Set
- [Algorithm] 448. Find All Numbers Disappeared in an Array
- [Algorithm] 88. Merge Sorted Array
- [Algorithm] How many times is a sorted array rotated?
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
- [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript
- [Algorithm] Heap data structure and heap sort algorithm
- [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function
- [Javascript] JavaScript Array Methods in Depth - push
- [Javascript] Array methods in depth - slice
- Index was outside the bounds of the array.
- [FAQ] Cannot use object of type MongoInt64 as array
- 成功解决TypeError: Cannot cast array data from dtype('float64') to dtype('U32') according to the rule '
- 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)