
I publish a few articles and tutorials each week, please consider entering your email here if you’d like to be added to my once-weekly email list. Therefor, if the remainder does not equal zero, we have an odd number: function isOdd(i) arr.findIndex(isOdd) // 4Īs expected, the index (4) of the odd number (9) is returned! Want more advanced JavaScript?Ĭheck out: A Beginners Guide to Advanced JavaScript An even number will always have a remainder of 0. To do this, we’ll check the remainder of the number divided by 2. Next, we need a function to determine if a number is odd. Here’s an example: console.log(users.find(item > item.id 2).name) // Jane console.log(users.find(item > item.name 'Jane'). What if we have an Array with all even numbers and one odd number? We can find the odd number with Array.find(). The find () method returns an object, not an array, so you need to reference the property and you don’t have to indicate an index. If it does, the value of that entry is returned. In the above example we are testing each entry in the array to see if it equals ‘b’. We’ll use an array with three entries: let arr = arr.find(k => k='b') // 'b'

If nothing passes, undefined is returned. Method 1: Array.findIndex() to find the search index.find() method executes a callback function once for each element in the array until it finds a value that returns true.

The Array.find() method returns the value of the first element in an array that passes a given test.
