admin 發表於 2022-11-15 16:04:13

JS : .findIndex() Method

We sometimes want to find the location of an element in an array. That’s where the .findIndex() method comes in! Calling .findIndex() on an array will return the index of the first element that evaluates to true in the callback function.
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];

const foundAnimal = animals.findIndex(animal => {
return animal === 'elephant';
});

const startsWithS = animals.findIndex(animal => {
return animal === 's';
});return 的是 index 的數字。
頁: [1]
查看完整版本: JS : .findIndex() Method