博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
每日一篇——lodash——array——chunk
阅读量:7128 次
发布时间:2019-06-28

本文共 2905 字,大约阅读时间需要 9 分钟。

_.chunk(array, [size=1])复制代码

将数组(array)拆分成多个 size 长度的区块,并将这些区块组成一个新数组。 如果array 无法被分割成全部等长的区块,那么最后剩余的元素将组成一个区块。

使用方法

chunk(['a', 'b', 'c', 'd'], 2) // => [['a', 'b'], ['c', 'd']] chunk(['a', 'b', 'c', 'd'], 3) // => [['a', 'b', 'c'], ['d']]复制代码

值得注意的是,这是一个纯函数,不会对传入的data有任何影响。

源码分析

var baseSlice = require('./_baseSlice'),    isIterateeCall = require('./_isIterateeCall'),    toInteger = require('./toInteger');/* Built-in method references for those with the same name as other `lodash` methods. */var nativeCeil = Math.ceil,    nativeMax = Math.max;/** * 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. * * @static * @memberOf _ * @since 3.0.0 * @category Array * @param {Array} array The array to process. * @param {number} [size=1] The length of each chunk * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. * @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, guard) {  if ((guard ? isIterateeCall(array, size, guard) : size === undefined)) {    size = 1;  } else {    size = nativeMax(toInteger(size), 0);  }  var length = array == null ? 0 : array.length;  if (!length || size < 1) {    return [];  }  var index = 0,      resIndex = 0,      result = Array(nativeCeil(length / size));  while (index < length) {    result[resIndex++] = baseSlice(array, index, (index += size));  }  return result;}module.exports = chunk;复制代码

这个方法整体实现没有很特别的地方,核心在于用Math.ceil对原数组分区块,然后循环切割原数组。while循环写得比较精简,将运算和赋值写在一起,对于不熟悉的人来说可能有点懵逼。这个方法有一个比较令人困惑的地方在于第三个参数guard以及isIterateeCall方法,这个参数在官网上没有提及,一般我们调用chunk方法都只会传前两个参数,只有当我们这整个方法作为map之类方法的参数时,才会有第三个参数,那么此时array就是当前元素,size就是index,而gard就变成了原数组,这种情况下会对原数组进行size=1的分割。当然不是只要有三个参数就可以认为这是Iteratee的情况,简而言之就是判断gard[size]是否等于array,具体的isIterateeCall实现可以看如下代码:

var eq = require('./eq'),    isArrayLike = require('./isArrayLike'),    isIndex = require('./_isIndex'),    isObject = require('./isObject');/** * Checks if the given arguments are from an iteratee call. * * @private * @param {*} value The potential iteratee value argument. * @param {*} index The potential iteratee index or key argument. * @param {*} object The potential iteratee object argument. * @returns {boolean} Returns `true` if the arguments are from an iteratee call, *  else `false`. */function isIterateeCall(value, index, object) {  if (!isObject(object)) {    return false;  }  var type = typeof index;  if (type == 'number'        ? (isArrayLike(object) && isIndex(index, object.length))        : (type == 'string' && index in object)      ) {    return eq(object[index], value);  }  return false;}module.exports = isIterateeCall;复制代码

转载于:https://juejin.im/post/5c8cb090f265da2dbf5f516d

你可能感兴趣的文章
号外:小雷将开发一款Java版的简易CMS系统
查看>>
Using ADSI Edit to Resolve Conflicting or Duplicate AD Integrated DNS zones
查看>>
用Eclipse搭建C/C++开发环境
查看>>
linux 一些好用的软件
查看>>
xls 扩展名与格式不一致
查看>>
kernel-devel安装
查看>>
centos6.4搭建rsyslog日志服务器和loganalyzer 日志分析工具
查看>>
线上MYSQL同步报错故障处理总结 实现同步不一致进行邮件报警
查看>>
powershell 跑EAC命令使用resultsize unlimited出错解决方法
查看>>
从文件中读取数据乱码
查看>>
[ganglia]关于无法显示监控画面的一种莫名的解决办法
查看>>
稀疏矩阵的压缩存储及转置
查看>>
MicroPython实例之TPYBoard v102炫彩跑马灯WS2812B
查看>>
机器能听懂人类歌声吗
查看>>
python随笔系列--global和nonlocal关键字
查看>>
Netstat 连接状态记录
查看>>
【服务端】Apache MiNa 实现多人聊天室
查看>>
Spring Boot @Scheduled 执行两遍
查看>>
我的友情链接
查看>>
2.2.0-ContainersMonitorImpl的报错解决
查看>>