Last Updated : 03 Sep, 2024
Lodash _.zipWIth() method is used to combine arrays into one array by applying a function in the same value at the same index of arrays first the function is applied on the first value of the array and then the returned value is added into a new array first value and same for the second, third and so on. If you don't pass the function it will simply work as zip.
Syntax:_.zipWith(arrays, [iteratee=_.identity]);Parameters:
Example 1: In this example, we are getting the sum of elements of the given array according to the function passed in the lodash _.zipWith() method.
javascript
const _ = require('lodash');
let x = [10, 20, 30];
let y = [100, 200, 300];
let combinedArray = _.zipWith(x, y, function (a, b) {
return a + b;
});
console.log(combinedArray);
Output:
[ 110, 220, 330 ]
Example: In this example, we are getting the concatenation of elements of the given array according to the function passed in the lodash _.zipWith() method.
javascript
const _ = require('lodash');
let firstname = ['Rahul', 'Ram', 'Aditya'];
let lastname = ['Sharma', 'Kumar', 'Verma'];
let fullname = _.zipWith(firstname, lastname, function (a, b) {
return a + ' ' + b;
});
console.log(fullname);
Output:
[ 'Rahul Sharma', 'Ram Kumar', 'Aditya Verma' ]
Example: In this example, we are getting the same array as the given array because we are not passing any custom function in the lodash _.zipWith() method.
javascript
const _ = require('lodash');
let x = [10, 20, 30];
let y = [100, 200, 300];
let z = [1000, 2000, 3000];
let combinedArray = _.zipWith(x, y, z);
console.log(combinedArray);
Output:
[ [ 10, 100, 1000 ], [ 20, 200, 2000 ], [ 30, 300, 3000 ] ]
Note: This will not work in normal JavaScript because it requires the library lodash to be installed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4