how to use the unzipwith function from the lodash library in typescript

To use the unzipWith function from the Lodash library in TypeScript, you first need to install the package using the following command:

index.ts
npm install --save lodash
26 chars
2 lines

After that, you can import the unzipWith function from the lodash package in your TypeScript file as follows:

index.ts
import * as _ from "lodash";

const zipped: [string[], number[]] = [['a', 1], ['b', 2], ['c', 3]];
const result = _.unzipWith(zipped, (arr) => {
    return arr.reduce((acc, curr) => acc + curr, 0);
});

console.log(result);
224 chars
9 lines

In the above example, we have created a zipped array with two sub-arrays containing string and number values. We passed this array as the first argument to the unzipWith function along with a custom function as the second argument. The custom function takes each sub-array as an argument, and we have used the reduce function to sum up the values in each sub-array. Finally, we log the result to the console.

The unzipWith function returns a new array with unziped values after applying the custom function to each array.

gistlibby LogSnag