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

To use the includes function from the Underscore library in JavaScript, first, you need to install the library. You can do that by using a package manager like npm:

npm install underscore
23 chars
2 lines

Once you have installed the library, you can then include it in your JavaScript code using the require statement:

index.tsx
const _ = require('underscore');
33 chars
2 lines

Then, you can use the includes function from the Underscore library to check if an element exists in an array or not. The includes function takes two parameters: the array to search and the value to search for. It returns a boolean value indicating whether the value was found in the array or not.

Here is an example of how to use the includes function:

index.tsx
const arr = [1, 2, 3, 4, 5];

// check if 3 exists in the array
const result = _.includes(arr, 3);
console.log(result); // true

// check if 6 exists in the array
const result2 = _.includes(arr, 6);
console.log(result2); // false
230 chars
10 lines

In the above example, we first define an array arr with some values. Then, we use the _.includes function to check if the value 3 exists in the array, and it returns true. Similarly, we check if the value 6 exists in the array, and it returns false.

gistlibby LogSnag