how to use the each function from the underscore library in typescript

To use the each function from the Underscore library in TypeScript, first install the Underscore library:

npm install underscore
23 chars
2 lines

Then import the each function and use it on an array:

index.ts
import { each } from 'underscore';

const array = [1, 2, 3];

each(array, (value: number, index: number) => {
  console.log(`Value at index ${index} is ${value}`);
});
168 chars
8 lines

The each function takes two arguments: an array to iterate over and a function to invoke for each element in the array. The function receives two arguments - the current value and index of the element being iterated over.

Note the use of type annotations to ensure that the value and index arguments are of the correct type.

gistlibby LogSnag