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

To use the includes function from the Underscore library in TypeScript, you will first need to install Underscore using npm:

npm install underscore
23 chars
2 lines

Then, you can import Underscore and use the includes function like this:

index.ts
import * as _ from 'underscore';

const arr: string[] = ['apple', 'banana', 'orange'];

if (_.includes(arr, 'banana')) {
  console.log('The array includes "banana"');
} else {
  console.log('The array does not include "banana"');
}
232 chars
10 lines

In this example, we import Underscore and alias it as _. We then declare an array arr and use the _.includes function to check if the array includes the string 'banana'. The function returns a boolean value indicating whether the array includes the specified value.

Note that the includes function works with arrays, strings, and objects, and can also be used in a functional programming style with higher-order functions such as map, reduce, and filter.

gistlibby LogSnag