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

To use the indexOf() function from the Underscore library to search for an element in an array, you can do the following:

  1. First, include the Underscore library in your JavaScript file by adding the following code at the top:
index.tsx
<script src="https://underscorejs.org/underscore-min.js"></script>
67 chars
2 lines
  1. Next, create an array and assign it to a variable:
index.tsx
var myArray = [1, 2, 3, 4, 5];
31 chars
2 lines
  1. To search for an element in the array using the indexOf() function, call it on the Underscore object and pass in the array and the element you want to find as arguments:
index.tsx
var index = _.indexOf(myArray, 3);
35 chars
2 lines

The variable index will now contain the index of the element 3 in the array myArray. If the element is not found, the function will return -1.

gistlibby LogSnag