轉自或參考:js數組遍歷方法總結
https://www.cnblogs.com/woshidouzia/p/9304603.htmljavascript
數組遍歷方法html
1.for循環java
使用臨時變量,將長度緩存起來,避免重複獲取數組長度,當數組較大時優化效果纔會比較明顯。數組
for(j = 0,len=arr.length; j < len; j++) { }
2.foreach循環緩存
遍歷數組中的每一項,沒有返回值,對原數組沒有影響,不支持IE函數
//1 沒有返回值 arr.forEach((item,index,array)=>{ //執行代碼 }) //參數:value數組中的當前項, index當前項的索引, array原始數組; //數組中有幾項,那麼傳遞進去的匿名回調函數就須要執行幾回;
3.map循環測試
有返回值,能夠return出來優化
map的回調函數中支持return返回值;return的是啥,至關於把數組中的這一項變爲啥(並不影響原來的數組,只是至關於把原數組克隆一份,把克隆的這一份的數組中的對應項改變了);ui
arr.map(function(value,index,array){ //do something return XXX })
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遍歷this
能夠正確響應break、continue和return語句
for (var value of myArray) { console.log(value); }
5.filter遍歷
不會改變原始數組,返回新數組
var arr = [ { id: 1, text: 'aa', done: true }, { id: 2, text: 'bb', done: false } ] console.log(arr.filter(item => item.done))
轉爲ES5
arr.filter(function (item) { return item.done; });
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。
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。
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.some( function( item, index, array ){ return item > 3; })); true
8.reduce
reduce()
方法接收一個函數做爲累加器(accumulator),數組中的每一個值(從左到右)開始縮減,最終爲一個值。
var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10
reduce
接受一個函數,函數有四個參數,分別是:上一次的值,當前值,當前值的索引,數組
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){ return previousValue + currentValue; });
reduce
還有第二個參數,咱們能夠把這個參數做爲第一次調用callback
時的第一個參數,上面這個例子由於沒有第二個參數,因此直接從數組的第二項開始,若是咱們給了第二個參數爲5,那麼結果就是這樣的:
[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
等於數組中倒數第二個值。
var arr = [0,1,2,3,4]; arr.reduceRight(function (preValue,curValue,index,array) { return preValue + curValue; }); // 10
回調將會被調用四次,每次調用的參數及返回值以下:
若是提供一個初始值initialValue
爲5
:
var arr = [0,1,2,3,4]; arr.reduceRight(function (preValue,curValue,index,array) { return preValue + curValue; }, 5); // 15
回調將會被調用五次,每次調用的參數及返回的值以下:
一樣的,能夠對一個數組求和,也可使用reduceRight()
方法:
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
var stu = [ { name: '張三', gender: '男', age: 20 }, { name: '王小毛', gender: '男', age: 20 }, { name: '李四', gender: '男', age: 20 } ]
function getStu(element){ return element.name == '李四' } stu.find(getStu) //返回結果爲 //{name: "李四", gender: "男", age: 20}
ES6方法
stu.find((element) => (element.name == '李四'))
11.findIndex
對於數組中的每一個元素,findIndex 方法都會調用一次回調函數(採用升序索引順序),直到有元素返回 true。只要有一個元素返回 true,findIndex 當即返回該返回 true 的元素的索引值。若是數組中沒有任何元素返回 true,則 findIndex 返回 -1。
findIndex 不會改變數組對象。
[1,2,3].findIndex(function(x) { x == 2; }); // Returns an index value of 1.
[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()是對鍵值對的遍歷
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"