ES5 陣列的擴充套件方法

2021-10-08 14:09:02 字數 993 閱讀 4570

/*

* array.prototype.indexof() //返回值為陣列第乙個元素的下標,未找到就會返回-1

* array.prototype.lastindexof() //返回值為陣列最後乙個元素的下標

* array.prototype.foreach(funcation(){})//遍歷陣列

* array.prototype.map(funcation(){})//返回乙個新的陣列,對原陣列每個元素進行處理

* array.prototype.filter(funcation(){}) //返回滿足條件為true的新陣列

* * 繫結到顯示原型上的方法,供例項進行使用的方法

* */

var arr =[0

,2,0

,3,5

,10,8

,10] console.

log(arr.

indexof(0

))//0 陣列中有兩個0 返回第乙個0的下標

console.

log(arr.

lastindexof(10

))//7 陣列中有兩個10,返回最後乙個10的下標

arr.

foreach

(function

(item, index)

)var arr1 = arr.

map(

function

(item, index)

) console.

log(arr1)

// [2, 4, 2, 5, 7, 12, 10, 12]

var arr2 = arr1.

filter

(function

(item, index)

) console.

log(arr2)

//[4, 5, 7, 12, 10, 12]

<

/script>

ES5 陣列的擴充套件

array.prototype.indexof value 得到值在陣列中的第乙個下標 array.prototype.lastindexof value 得到值在陣列中的最後乙個下標 array.prototype.foreach function item,index 遍歷陣列 array.pr...

ES5陣列的方法

map 對映 語法 陣列.map function item,index,arr item 陣列中每乙個資料 index 索引 arr 原始陣列 foreach 迴圈 陣列.foreach function item,index,arr filter 過濾器 陣列.filter function i...

ES5陣列的方法

varary 1,23,4 functionisarray o console.log isarray ary isarray 檢測是否是陣列。tostring 把陣列轉換為字串,並返回結果。tolocalestring 把陣列轉換為本地陣列,並返回結果。valueof 返回陣列物件的原始值。tos...