找回密碼
 立即註冊
查看: 1532|回復: 0

JS : .findIndex() Method

[複製鏈接]

920

主題

629

回帖

3萬

積分

管理員

論壇管理員

積分
37702

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

發表於 2022-11-15 16:04:13 | 顯示全部樓層 |閱讀模式
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.
  1. const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];

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

  5. const startsWithS = animals.findIndex(animal => {
  6.   return animal[0] === 's';
  7. });
複製代碼
return 的是 index 的數字。
[發帖際遇]: admin 發帖時在路邊撿到 1 金錢,偷偷放進了口袋. 幸運榜 / 衰神榜
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DD論壇 維護: Redd Design

GMT+8, 2024-5-6 00:16 , Processed in 0.084816 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表