js实现 chunk 函数分组数组

复制代码
// 自己实现
function
chunk(list, size) { let len = list.length; if (size < 1 || !len) { return []; } if (size > len) { return [list]; } // 最终返回数组 let res = []; // 向下取整 let integer = Math.floor(len / size); // 取余 let rest = len % size; for (let i = 1; i <= integer; i++) { res.push(list.splice(0, size)); } if (rest) { res.push(list.splice(0, rest)); } return res; } // console.log(chunk([1, 2, 3, 4, 5], 0)); console.log(chunk([1, 2, 3, 4, 5], -1)); console.log(chunk([1, 2, 3, 4, 5], 2)); console.log(chunk([1, 2, 3, 4, 5], 3)); console.log(chunk([1, 2, 3, 4, 5], 4)); console.log(chunk([1, 2, 3, 4, 5], 5)); console.log(chunk([1, 2, 3, 4, 5], 6)); // [] // [] // [ [ 1, 2 ], [ 3, 4 ], [ 5 ] ] // [ [ 1, 2, 3 ], [ 4, 5 ] ] // [ [ 1, 2, 3, 4 ], [ 5 ] ] // [ [ 1, 2, 3, 4, 5 ] ] // [ [ 1, 2, 3, 4, 5 ] ]
复制代码

lodash chunk源码(供参考):https://github.com/lodash/lodash/blob/master/chunk.js

复制代码
import slice from './slice.js'
import toInteger from './toInteger.js'

/**
 * Creates an array of elements split into groups the length of `size`.
 * If `array` can't be split evenly, the final chunk will be the remaining
 * elements.
 *
 * @since 3.0.0
 * @category Array
 * @param {Array} array The array to process.
 * @param {number} [size=1] The length of each chunk
 * @returns {Array} Returns the new array of chunks.
 * @example
 *
 * chunk(['a', 'b', 'c', 'd'], 2)
 * // => [['a', 'b'], ['c', 'd']]
 *
 * chunk(['a', 'b', 'c', 'd'], 3)
 * // => [['a', 'b', 'c'], ['d']]
 */
function chunk(array, size = 1) {
  size = Math.max(toInteger(size), 0)
  const length = array == null ? 0 : array.length
  if (!length || size < 1) {
    return []
  }
  let index = 0
  let resIndex = 0
  const result = new Array(Math.ceil(length / size))

  while (index < length) {
    result[resIndex++] = slice(array, index, (index += size))
  }
  return result
}

export default chunk
复制代码

 

posted @   蓓蕾心晴  阅读(889)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2018-08-22 react给一个div行内加背景图片并实现cover覆盖模式居中显示
2018-08-22 react获取当前页面的url参数
点击右上角即可分享
微信分享提示