Chunk Array 陣列切割分塊
Given an array arr
and a chunk size size
, return a chunked array. A chunked array contains the original elements in arr
, but consists of subarrays each of length size
. The length of the last subarray may be less than size
if arr.length
is not evenly divisible by size
.
You may assume the array is the output of JSON.parse
. In other words, it is valid JSON.
Please solve it without using lodash's _.chunk
function.
Example 1:
Input: arr = [1,2,3,4,5], size = 1
Output: [[1],[2],[3],[4],[5]]
Explanation: The arr has been split into subarrays each with 1 element.
Example 2:
Input: arr = [1,9,6,3,2], size = 3
Output: [[1,9,6],[3,2]]
Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
Example 3:
Input: arr = [8,5,3,2,6], size = 6
Output: [[8,5,3,2,6]]
Explanation: Size is greater than arr.length thus all elements are in the first subarray.
Example 4:
Input: arr = [], size = 1
Output: []
Explanation: There are no elements to be chunked so an empty array is returned.
Constraints:
arr is a valid JSON array
2 <= JSON.stringify(arr).length <= 105
1 <= size <= arr.length + 1
解法:
這是在講給定一個陣列,跟他指定要分每個區塊的數量
遍例陣列,在讀他分塊的數量有無有存在,有就把el推進去這個塊,沒有在推一個空塊的並把當前el帶入
const chunk = (arr: any[], size: number) => {
const res: any[][] = [];
for(let i = 1; i <= arr.length; i++){
const scope = Math.ceil(i/size) - 1;
if(res[scope]){
res[scope].push(arr[i - 1]);
}else{
res.push([arr[i - 1]]);
}
}
return res;
};