js陣列遍歷方法總結
js陣列遍歷方法總結
陣列遍歷方法
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"
|