uses findIndex method and displays index value
Fri Aug 16 2024 01:45:34 GMT+0000 (Coordinated Universal Time)
Saved by
@thecowsays
#javascript
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
const foundAnimal = animals.findIndex(a => {
return a === 'elephant';
});
console.log(animals[foundAnimal]); // uses function's returned index to display value
const startsWithS = animals.findIndex(letter => {
return letter[0] === 's';
});
console.log(startsWithS); // function returns index number of first TRUE element
console.log(animals[startsWithS]); // used to display that element's value
content_copyCOPY
Example of how to use .findIndex array method, but also how to display the value of the found index element by nesting.
Comments