[Algorithm] Chunk Array
// --- 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]]);
});
[Algorithm] Chunk Array的更多相关文章
- [Algorithm] Reverse array of Chars by word
For example we have: ["p", "r", "e", "f", "e", &qu ...
- lodash框架中的chunk与drop函数源码逐行分析
lodash是一个工具库,跟underscore差不多 chunk函数的作用: 把一维数组,按照固定的长度分段成二维数组 如: chunk( [ 10, 20, 30, 40 ], 2 ) 结 ...
- lodash源码分析之chunk的尺与刀
以不正义开始的事情,必须用罪恶使它巩固. --莎士比亚<麦克白> 最近很多事似乎印证了这句话,一句谎言最后要用一百句谎言来圆谎. 本文为读 lodash 源码的第二篇,后续文章会更新到这个 ...
- js eventLoop (使用chunk 同步变异步)
https://www.cnblogs.com/xiaohuochai/p/8527618.html 线程 javascript是单线程的语言,也就是说,同一个时间只能做一件事.而这个单线程的特性,与 ...
- STL容器之Array[转]
转自https://blog.csdn.net/sin_geek/article/details/51067874 作者 Sin_Geek 简介 array在头文件<array> 中定义 ...
- 硬刚 lodash 源码之路,_.chunk
前置 chunk 函数内部借助其他函数实现,所以从其他函数开始,chunk 在最后. 你可能需要一些 JavaScript 基础知识才能看懂一些没有注释的细节. isObject 判断是否为 Obje ...
- 超实用的 JavaScript 代码片段( ES6+ 编写)
Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...
- 模板化的七种排序算法,适用于T* vector<T>以及list<T>
最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...
- Design and Analysis of Algorithms_Brute Froce
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
随机推荐
- 对JSON.parse()中存在转义字符的解决以及js中替换函数replace()的认识
在工作中,遇到对页面数据进行转存json格式数据后存储在数据库中.然而在显示数据时遇到无法显示json中的数据,产生的bug 问题抛出: 1.首先认识下,在JSON.parse()将后台传过来的字符串 ...
- shell如果文件夹不存在则创建
#!/bin/bash build_dir="build" if [ ! -d "$build_dir" ]; then mkdir $build_dir fi ...
- kafka安装、相关命令以及PHP使用
1.安装JAVA #下载安装包 https://www.oracle.com/technetwork/java/javase/downloads/index.html tar -xzvf jdk-8u ...
- 第十一章 ZYNQ-MIZ701 PS读写PL端BRAM
本篇文章目的是使用Block Memory进行PS和PL的数据交互或者数据共享,通过zynq PS端的Master GP0端口向BRAM写数据,然后再通过PS端的Mater GP1把数据读出来,将 ...
- 【广搜】Knight Moves
题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...
- 怎样获取所有的script节点
1. 使用document.scripts; document.scripts instanceof HTMLCollection; // true 2. 使用 document.getElement ...
- python 比对PDF文件
基本思路: 1.读取pdf内容,存放到不同的 list 2.比较 list 的相似度 ------------------------ 实现------------------------- 1.PD ...
- JS基础_函数的参数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 买条Vineyard Vines裙子为啥子那么难?因为能遮胖?因为英国王子穿过?
为了这件vineyard vines, 我周六冒雨,去斯坦福shopping center说卖完了,我冒雨赶回家,上网买到了,今天早上发email说没货了,自动取消我的订单.我下午又打了40分钟电话给 ...
- js之数据类型(对象类型——单体内置对象——Math)
Math是一个内置对象,它具有数学常数和函数的属性和方法.Math对象用于执行数学任务,和其它对象不同,Math只是一个静态对象并没有Math()构造函数,实际上,Math()只是一个由js设置的对象 ...