当前位置:网站首页>Group arrays by a specified size
Group arrays by a specified size
2022-06-11 06:57:00 【Xiaoman's code world】
Use Array.from() Create a new array , This matches the number of blocks that will be generated . Use Array.slice() Map each element of the new array to size Blocks of length . If the original array cannot be split evenly , Then the final block will contain the remaining elements .
let chunk = (arr, size) => Array.from({
length: Math.ceil(arr.length / size)
}, (v, i) => arr.slice(i * size, i * size + size));
let arr = [1, 2, 3, 4];
console.log(chunk(arr,2)) //0: Array [ 1, 4 ],1: Array [ 3,4 ],
边栏推荐
- 【迅为干货】龙芯2k1000开发板opencv 测试
- The difference between TCP and UDP
- How to make time planning
- Post exam summary
- Esp32 learning notes (49) - esp-wifi-mesh interface use
- Leetcode hot topic 100 topic 6-10 solution
- JS implementation of Hill sort of graphic insertion sort [with source code]
- Difference between byte and bit
- [probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen
- Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]
猜你喜欢

Shutter restraint container assembly

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally

Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch

UEFI查找PCI设备

Common troubleshooting tools and analysis artifacts are worth collecting

The realization of online Fox game server room configuration battle engagement customization function

. Net C Foundation (6): namespace - scope with name

Use of qscriptengine class

Flat design, blog website (VIII) code source code

常用问题排查工具和分析神器,值得收藏
随机推荐
Flat design, blog website (VIII) code source code
Mongodb installation
【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
Required reading 1: the larger the pattern, the better they know how to speak
Lazy load
Shutter restraint container assembly
Oracle prompt invalid number
Starting from scratch (I)
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
Difference between byte and bit
Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)
Dynamic import
Wan Zichang's article shows you promise
河南高考VS天津高考(2008年-2021年)
About daily report plan
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
Prototype and prototype chain
Shell脚本之启动Nacos服务端
538. convert binary search tree to cumulative tree
Throttling and anti shake