The zipWith
function from Lodash creates an array of elements defined by iterating over several arrays, and invokes the provided function with the corresponding elements of each array.
Here's an example of how to use zipWith
:
index.tsx217 chars10 lines
In this example, we're passing in three arrays, arr1
, arr2
, and arr3
. We're then defining a function that will be called with the corresponding elements of each array. The function takes three parameters, a
, b
, and c
, which represent the elements of each array at the current index. In this case, we're simply summing the values of a, b, and c.
The zipWith
function returns an array that contains the results of our function called with the corresponding elements of each array. In this case, the resulting array is [12, 15, 18]
.
Note that zipWith
will stop iterating when the shortest array has been exhausted, so make sure that all the arrays you're passing in are of the same length if you want to ensure that all elements are used.
gistlibby LogSnag