To find an item in a list (array) in JavaScript, you can use the indexOf
method. The indexOf
method returns the index of the first occurrence of the specified item in an array, or -1 if not found.
Here's an example code snippet:
index.tsx270 chars11 lines
In this example, we have an array myList
and a variable itemToFind
. We use the indexOf
method to find the index of itemToFind
in the myList
array. We then check if the item was found, and if so, log a message to the console indicating the index at which it was found. If the item was not found, we log a different message to the console.
gistlibby LogSnag