小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

lodash函數(shù)庫 -- chunk

 頭號碼甲 2021-08-28

loadsh函數(shù)庫中的 chunk 函數(shù)采用 typescript 語法重寫.

chunk 函數(shù)
將數(shù)組(array)拆分成多個(gè) size 長度的區(qū)塊,并將這些區(qū)塊組成一個(gè)新數(shù)組。
如果array 無法被分割成全部等長的區(qū)塊,那么最后剩余的元素將組成一個(gè)區(qū)塊。

/**
 *
 * 將數(shù)組(array)拆分成多個(gè) size 長度的區(qū)塊,并將這些區(qū)塊組成一個(gè)新數(shù)組。
 * 如果array 無法被分割成全部等長的區(qū)塊,那么最后剩余的元素將組成一個(gè)區(qū)塊。
 *
 * @param array 需要處理的數(shù)組
 * @param size 每個(gè)數(shù)組區(qū)塊的長度
 * @returns {Array<Array<T>>}
 * @example
 *
 * chunk(['a', 'b', 'c'], 2)
 * // => [['a', 'b'], ['c']]
 *
 * chunk(['a', 'b', 'c','d'], 2)
 * // => [['a', 'b'], ['c','d']]
 */
const chunk = <T>(array: Array<T>, size = 1): Array<Array<T>> => {
// 邊界檢測
// 需要考慮塊長度小于1和空數(shù)組
const length = array.length;
if (!length || size < 1) return [];

let index = 0; // 處理數(shù)組起始位
let resindex = 0; // 新數(shù)組起始位
const result = new Array<Array<T>>(Math.ceil(length / size));
while (index < length) {
result[resindex++] = array.slice(index, (index += size)); // size 為步長值
}
return result;
};

export default chunk;
import chunk from "../src/chunk";

const arr = [1, 2, 3, 4, 5, 6];
const n = chunk(arr, 3);
console.log(n); // [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]

const arrs = ["a", "b", "c"];
const n1 = chunk(arrs, 2);
console.log(n1); // [ [ 'a', 'b' ], [ 'c' ] ]

const arre = [] as Array<string>;
const n2 = chunk(arre, 2);
console.log(n2); // []

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多