How to use the unzipWith function from lodash

Find comprehensive JavaScript lodash.unzipWith code examples handpicked from public code repositorys.

lodash.unzipWith is a function in the Lodash library that unzips a matrix of arrays into a set of arrays and applies a custom function to each group of corresponding elements.

420
421
422
423
424
425
426
427
428
429
module.exports.unset               = _.unset;
module.exports.unsplat             = _.unsplat;
module.exports.unsplatl            = _.unsplatl;
module.exports.unsplatr            = _.unsplatr;
module.exports.unzip               = _.unzip;
module.exports.unzipWith           = _.unzipWith;
module.exports.update              = _.update;
module.exports.updatePath          = _.updatePath;
module.exports.updateWith          = _.updateWith;
module.exports.upperCase           = _.upperCase;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

171
172
173
174
175
176
177
178
179
180
181
182
183
console.log(uniqWith); // => [2.1, 1.2]


const unzip = _.unzip([['a', 1, true], ['b', 2, false]]);
console.log(unzip); // => [['a', 'b'], [1, 2], [true, false]]


const unzipWith = _.unzipWith([[1, 10, 100], [2, 20, 200]], (...arrays) => _.sum(arrays));
console.log(unzipWith); // => [3, 30, 300]


const without = _.without([2, 1, 2, 3], 1, 2);
console.log(without); // => [3]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.unzipWith work?

lodash.unzipWith is a method in the Lodash library that returns a new array of arrays created by unzipping the input arrays and invoking the given function with the corresponding elements from each input array. The function is called for each element of the resulting arrays, where each argument passed to the function is an array of corresponding elements from each input array at that index. The function can be used to combine or transform the values before they are returned in the resulting arrays.

492
493
494
495
496
497
498
499
500
501
502
503


//_.unzipWith(array,[iteratee=_.indentity])
let zipped1 = _.zip([1, 2], [10, 20], [100, 200]);
console.log('zipped1--->', zipped1);
//zipped1---> [ [ 1, 10, 100 ], [ 2, 20, 200 ] ]
let unzipWith1 = _.unzipWith(zipped1, _.add);
console.log('unzipWith1--->', unzipWith1);
//unzipWith1---> [ 3, 30, 300 ]


//_.with(array,[values])
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("lodash");

const arr = [
  [1, 2, 3],
  [4, 5, 6],
];
const result = _.unzipWith(arr, (a, b) => a + b);

console.log(result); // Output: [5, 7, 9]

In this example, we have an array arr that contains two arrays with three elements each. We use _.unzipWith to unzip the arrays and combine them using the provided function (a, b) => a + b. This function takes two arguments, a and b, which are the elements from the corresponding positions of the two arrays. In this case, the function adds the elements together to produce a new array [5, 7, 9], which is the result of the _.unzipWith operation.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)