_.unzip(array)
55
_.unzip(array)
_.unzip会把zip方法的结果转换回初始状态
_.zip方法接收多个数组,取每个参数数组的第一个元素作为一个数组,这个数组是结果数组的第一个元素;取每个参数数组的第二个元素作为一个数组,这个数组是结果数组的第二个元素;后面以此类推
参数
array (Array): 需要逆转zip操作的数组
返回值
(Array): 逆转zip操作后的数组
例子
var zipped = _.zip(['a', 'b'], [1, 2], [true, false]); // => [['a', 1, true], ['b', 2, false]] _.unzip(zipped); // => [['a', 'b'], [1, 2], [true, false]]
源代码:
import filter from './filter.js' import map from './map.js' import baseProperty from './.internal/baseProperty.js' import isArrayLikeObject from './isArrayLikeObject.js' /** * This method is like `zip` except that it accepts an array of grouped * elements and creates an array regrouping the elements to their pre-zip * configuration. * * @since 1.2.0 * @category Array * @param {Array} array The array of grouped elements to process. * @returns {Array} Returns the new array of regrouped elements. * @see unzipWith, zip, zipObject, zipObjectDeep, zipWith * @example * * const zipped = zip(['a', 'b'], [1, 2], [true, false]) * // => [['a', 1, true], ['b', 2, false]] * * unzip(zipped) * // => [['a', 'b'], [1, 2], [true, false]] */ //unzip会把zip方法的结果转换回初始状态 //zip方法接收多个数组,取每个参数数组的第一个元素作为一个数组,这个数组是结果数组的第一个元素;取每个参数数组的第二个元素作为一个数组,这个数组是结果数组的第二个元素;后面以此类推 function unzip(array) { if (!(array != null && array.length)) {//如果数组为空,或者数组没有长度,返回空数组 return [] } let length = 0//结果数组的长度 array = filter(array, (group) => {//用filter遍历array,过滤掉非array-like对象,并且计算出结果数组的长度 if (isArrayLikeObject(group)) { length = Math.max(group.length, length) return true } }) let index = -1//循环索引 const result = new Array(length)//结果数组 while (++index < length) {//结果数组的元素是分别从每一个array元素取出对应的索引值 result[index] = map(array, baseProperty(index)) } return result } export default unzip
filter
/** * Iterates over elements of `array`, returning an array of all elements * `predicate` returns truthy for. The predicate is invoked with three * arguments: (value, index, array). * * **Note:** Unlike `remove`, this method returns a new array. * * @since 5.0.0 * @category Array * @param {Array} array The array to iterate over. * @param {Function} predicate The function invoked per iteration. * @returns {Array} Returns the new filtered array. * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject * @example * * const users = [ * { 'user': 'barney', 'active': true }, * { 'user': 'fred', 'active': false } * ] * * filter(users, ({ active }) => active) * // => objects for ['barney'] */ //和原生的filter方法类似,遍历数组,将predicate处理后结果为真值的元素组成一个新数组返回 function filter(array, predicate) { let index = -1//循环索引 let resIndex = 0 const length = array == null ? 0 : array.length//数组长度 const result = []//结果数组 while (++index < length) { const value = array[index] if (predicate(value, index, array)) {//如果predicate返回true,就把当前元素加入结果数组 result[resIndex++] = value } } return result } export default filter
map
/** * Creates an array of values by running each element of `array` thru `iteratee`. * The iteratee is invoked with three arguments: (value, index, array). * * @since 5.0.0 * @category Array * @param {Array} array The array to iterate over. * @param {Function} iteratee The function invoked per iteration. * @returns {Array} Returns the new mapped array. * @example * * function square(n) { * return n * n * } * * map([4, 8], square) * // => [16, 64] */ //对数组每一个元素执行迭代器后返回由返回值组成的新数组 function map(array, iteratee) { let index = -1//循环索引 const length = array == null ? 0 : array.length//数组长度 const result = new Array(length)//结果数组 while (++index < length) {//循环调用迭代器,把处理后的返回值存入结果数组 result[index] = iteratee(array[index], index, array) } return result } export default map
baseProperty
/** * The base implementation of `property` without support for deep paths. * * @private * @param {string} key The key of the property to get. * @returns {Function} Returns the new accessor function. */ //property方法基础实现,不支持深层处理 function baseProperty(key) {//返回一个获取对象上对应key的值的函数 return (object) => object == null ? undefined : object[key] } export default baseProperty
isArrayLikeObject
import isArrayLike from './isArrayLike.js' import isObjectLike from './isObjectLike.js' /** * This method is like `isArrayLike` except that it also checks if `value` * is an object. * * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is an array-like object, * else `false`. * @example * * isArrayLikeObject([1, 2, 3]) * // => true * * isArrayLikeObject(document.body.children) * // => true * * isArrayLikeObject('abc') * // => false * * isArrayLikeObject(Function) * // => false */ //判断一个值是不是一个array-like对象 //isObjectLike判断一个值是否是一个object-like,规则是:typeof返回object,并且不是null //isArrayLike判断一个值是否是一个array-like,规则:不等于null,不是function类型,并且有length属性,length是大于0小于Number.MAX_SAFE_INTEGER的整数 function isArrayLikeObject(value) { return isObjectLike(value) && isArrayLike(value) } export default isArrayLikeObject

浙公网安备 33010602011771号