iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0
自我挑戰組

JavaScript Array x 學習筆記系列 第 14

[Day 14 | Array.prototype.lastIndexOf () ]

array.lastIndexOf()

昨天我們學習了 array.indexOf()

功能找到陣列裡第一個出現的指定元素,

而 lastIndexOf 剛好相反,

lastIndexOf 是回傳陣列裡最後一個出現的指定元素。


Syntax

array.lastIndexOf(searchElement[, fromIndex]);
  • searchElement - 要尋找的值

  • fromIndex - 從陣列的哪個位置開始找,可省略(預設位置為陣列的長度)


Example

程式碼如下:

const animals = ['dolphin', 'Tiger', 'Penguin', 'dolphin', 'elephant', 'dolphin', 'duck'];

console.log(animals.lastIndexOf('dolphin')) 

console.log(animals.lastIndexOf('cat'))

console.log(animals.lastIndexOf('dolphin',2))

參考資料 https://www.tutorialspoint.com/javascript/array_lastindexof.htm


上一篇
[Day 13 | Array.prototype.indexOf () - 簡易搜尋 ]
下一篇
[Day 15 | Array.prototype.filter () ]
系列文
JavaScript Array x 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言