數組遍歷方法
1.for循環
使用臨時變量,將長度緩存起來,避免重複獲取數組長度,當數組較大時優化效果纔會比較明顯。
1
2
3
|
for (j = 0,len=arr.length; j < len; j++) { } |
2.foreach循環
遍歷數組中的每一項,沒有返回值,對原數組沒有影響,不支持IE
1
2
3
4
5
6
|
//1 沒有返回值 arr.forEach((item,index,array)=>{ //執行代碼 }) //參數:value數組中的當前項, index當前項的索引, array原始數組; //數組中有幾項,那麼傳遞進去的匿名回調函數就需要執行幾次; |
3.map循環
有返回值,可以return出來
map的回調函數中支持return返回值;return的是啥,相當於把數組中的這一項變爲啥(並不影響原來的數組,只是相當於把原數組克隆一份,把克隆的這一份的數組中的對應項改變了);
1
2
3
4
5
6
7
|
arr.map( function (value,index,array){ //do something return XXX }) |
1
2
3
4
5
6
|
var ary = [12,23,24,42,1]; var res = ary.map( function (item,index,ary ) { return item*10; }) console.log(res); //-->[120,230,240,420,10]; 原數組拷貝了一份,並進行了修改 console.log(ary); //-->[12,23,24,42,1]; 原數組並未發生變化 |
4.forof遍歷
可以正確響應break、continue和return語句
1
2
3
|
for ( var value of myArray) { console.log(value); } |
5.filter遍歷
不會改變原始數組,返回新數組
1
2
3
4
5
|
var arr = [ { id: 1, text: 'aa' , done: true }, { id: 2, text: 'bb' , done: false } ] console.log(arr.filter(item => item.done)) |
轉爲ES5
1
2
3
|
arr.filter( function (item) { return item.done; }); |
1
2
3
|
var arr = [73,84,56, 22,100] var newArr = arr.filter(item => item>80) //得到新數組 [84, 100] console.log(newArr,arr) |
6.every遍歷
every()是對數組中的每一項運行給定函數,如果該函數對每一項返回true,則返回true。
1
2
3
4
5
|
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.every( function ( item, index, array ){ return item > 3; })); false |
7.some遍歷
some()是對數組中每一項運行指定函數,如果該函數對任一項返回true,則返回true。
1
2
3
4
5
6
|
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.some( function ( item, index, array ){ return item > 3; })); true |
8.reduce
reduce()
方法接收一個函數作爲累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終爲一個值。
1
|
var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10 |
reduce
接受一個函數,函數有四個參數,分別是:上一次的值,當前值,當前值的索引,數組
1
2
3
|
[0, 1, 2, 3, 4].reduce( function (previousValue, currentValue, index, array){ return previousValue + currentValue; }); |
reduce
還有第二個參數,我們可以把這個參數作爲第一次調用callback
時的第一個參數,上面這個例子因爲沒有第二個參數,所以直接從數組的第二項開始,如果我們給了第二個參數爲5,那麼結果就是這樣的:
1
2
3
|
[0, 1, 2, 3, 4].reduce( function (previousValue, currentValue, index, array){ return previousValue + currentValue; },5); |
第一次調用的previousValue
的值就用傳入的第二個參數代替,
9.reduceRight
reduceRight()
方法的功能和reduce()
功能是一樣的,不同的是reduceRight()
從數組的末尾向前將數組中的數組項做累加。
reduceRight()
首次調用回調函數callbackfn
時,prevValue
和 curValue
可以是兩個值之一。如果調用 reduceRight()
時提供了 initialValue
參數,則 prevValue
等於 initialValue
,curValue
等於數組中的最後一個值。如果沒有提供 initialValue
參數,則 prevValue
等於數組最後一個值, curValue
等於數組中倒數第二個值。
1
2
3
4
5
|
var arr = [0,1,2,3,4]; arr.reduceRight( function (preValue,curValue,index,array) { return preValue + curValue; }); // 10 |
回調將會被調用四次,每次調用的參數及返回值如下:
如果提供一個初始值initialValue
爲5
:
1
2
3
4
5
|
var arr = [0,1,2,3,4]; arr.reduceRight( function (preValue,curValue,index,array) { return preValue + curValue; }, 5); // 15 |
回調將會被調用五次,每次調用的參數及返回的值如下:
同樣的,可以對一個數組求和,也可以使用reduceRight()
方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var arr = [1,2,3,4,5,6]; console.time( "ruduceRight" ); Array.prototype.ruduceRightSum = function (){ for ( var i = 0; i < 10000; i++) { return this .reduceRight ( function (preValue, curValue) { return preValue + curValue; }); } } arr.ruduceRightSum(); console.log( '最終的值:' + arr.ruduceSum()); // 21 console.timeEnd( "ruduceRight" ); // 5.725ms |
10.find
find()方法返回數組中符合測試函數條件的第一個元素。否則返回undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
var stu = [ { name: '張三' , gender: '男' , age: 20 }, { name: '王小毛' , gender: '男' , age: 20 }, { name: '李四' , gender: '男' , age: 20 } ] |
1
2
3
4
5
6
7
|
function getStu(element){ return element.name == '李四' } stu.find(getStu) //返回結果爲 //{name: "李四", gender: "男", age: 20} |
ES6方法
1
|
stu.find((element) => (element.name == '李四' )) |
11.findIndex
對於數組中的每個元素,findIndex 方法都會調用一次回調函數(採用升序索引順序),直到有元素返回 true。只要有一個元素返回 true,findIndex 立即返回該返回 true 的元素的索引值。如果數組中沒有任何元素返回 true,則 findIndex 返回 -1。
findIndex 不會改變數組對象。
1
2
|
[1,2,3].findIndex( function (x) { x == 2; }); // Returns an index value of 1. |
1
2
|
[1,2,3].findIndex(x => x == 4); // Returns an index value of -1. |
12.keys,values,entries
ES6 提供三個新的方法 —— entries(),keys()和values() —— 用於遍歷數組。它們都返回一個遍歷器對象,可以用for...of循環進行遍歷,唯一的區別是keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
for ( let index of [ 'a' , 'b' ].keys()) { console.log(index); } // 0 // 1 for ( let elem of [ 'a' , 'b' ].values()) { console.log(elem); } // 'a' // 'b' for ( let [index, elem] of [ 'a' , 'b' ].entries()) { console.log(index, elem); } // 0 "a" // 1 "b" |