_.unzipWith(array, [iteratee=_.identity])
Array identity _.
2023-09-11 14:15:02 时间
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
相关文章
- [JavaScript] Array.prototype.reduce in JavaScript by example
- [Javascript] Refactoring: Array.prototype by example [filter, some, forEach]
- PHP中把stdClass Object转array的几个方法
- [Javascript] Create 2d array by using Array.from
- [Algorithm] 448. Find All Numbers Disappeared in an Array
- [ES2016] Check if an array contains an item using Array.prototype.includes
- 【Codeforces 1108E1】Array and Segments (Easy version)
- Python语言编程学习:numpy中的array格式数据切片与pandas中的dataframe格式数据切片、相互转换
- js hook array对象的push方法
- ClickHouse : arrayMap, arrayJoin or ARRAY JOIN memory usage
- 【算法题】输入一维数组array和n,找出和值为n的任意两个元素