_.unzipWith(array, [iteratee=_.identity])
56
_.unzipWith(array, [iteratee=_.identity])
_.unzipWith和unzip方法类似,多接收一个iteratee参数,在逆转zip操作之后对结果数组调用iteratee处理处最终结果
参数
array (Array): 需要逆转zip操作的数组
[iteratee=_.identity] (Function): 处理如何连接结果数组元素的遍历器
返回值
(Array): 逆转zip操作后的数组
例子
var zipped = _.zip([1, 2], [10, 20], [100, 200]); // => [[1, 10, 100], [2, 20, 200]] _.unzipWith(zipped, _.add); // => [3, 30, 300]
源代码:
import map from './map.js' import unzip from './unzip.js' /** * This method is like `unzip` except that it accepts `iteratee` to specify * how regrouped values should be combined. The iteratee is invoked with the * elements of each group: (...group). * * @since 3.8.0 * @category Array * @param {Array} array The array of grouped elements to process. * @param {Function} iteratee The function to combine * regrouped values. * @returns {Array} Returns the new array of regrouped elements. * @example * * const zipped = zip([1, 2], [10, 20], [100, 200]) * // => [[1, 10, 100], [2, 20, 200]] * * unzipWith(zipped, add) * // => [3, 30, 300] */ //和unzip方法类似,多接收一个iteratee参数,在逆转zip操作之后对结果数组调用iteratee处理处最终结果 function unzipWith(array, iteratee) { if (!(array != null && array.length)) {//如果数组为空或者数组没有长度,返回空数组 return [] } const result = unzip(array)//调用unzip获取到zip逆转后的结果 return map(result, (group) => iteratee.apply(undefined, group)) //遍历结果数组,用iteratee处理生成最终的结果 } export default unzipWith