數(shù)組遍歷
數(shù)組遍歷方法:forEach湃鹊,every荧呐,some,map痢虹,filter方法。
forEach:為數(shù)組中的每個(gè)元素調(diào)用定義的回調(diào)函數(shù)
every:檢查定義的回調(diào)函數(shù)是否為數(shù)組中的所有元素返回true
some:檢查定義的回調(diào)函數(shù)是否為數(shù)組的任何元素返回true
filter:每個(gè)數(shù)組項(xiàng)調(diào)用指定的函數(shù)主儡,條件滿足為true的將返回新的數(shù)組中
map:每個(gè)數(shù)組項(xiàng)調(diào)用指定的函數(shù)奖唯,返回每次調(diào)用的結(jié)果組成一個(gè)新數(shù)組
<code>forEach</code><code>every</code><code>some</code>方法不生成一個(gè)新數(shù)組,而<code>filter</code><code>map</code>方法將會生產(chǎn)一個(gè)符合條件的新數(shù)組糜值。這些方法都會調(diào)用指定的callbackfn丰捷∨髂回調(diào)函數(shù)語法如下:
forEach方法
語法:
array.forEach(callback[,thisArg]);
參數(shù)
callback為每個(gè)元素執(zhí)行,接受三個(gè)參數(shù):
currentValue 當(dāng)前值病往,數(shù)組正在處理的當(dāng)前元素捣染。
index 索引,數(shù)組正在處理的當(dāng)前元素的索引停巷。
array 正在應(yīng)用forEach()數(shù)組
thisArg 可選
當(dāng)執(zhí)行回調(diào)函數(shù)時(shí)作用this的值耍攘。
forEach方法按升序?yàn)閿?shù)組中含有效值的每一項(xiàng)執(zhí)行一次callback函數(shù),哪些已刪除或者從未賦值的項(xiàng)將被跳過叠穆,但不包括值為undefined的項(xiàng)少漆。
如果給forEach傳遞了thisArg參數(shù),它將作為callback函數(shù)執(zhí)行上下文硼被,類似執(zhí)行如下函數(shù)callback.call(thisArg,element,index,array)示损。如果thisArg值為undefined或null,函數(shù)的this值取決于當(dāng)前執(zhí)行環(huán)境是否為嚴(yán)格模式(嚴(yán)格模式為undefined嚷硫,非嚴(yán)格模式為全局對象)检访。
foreEach執(zhí)行一次callback函數(shù),總是返回undefined仔掸。
function logArrayElements(element,index,array){
console.log("a["+index+"]="+element);
}
var returnValue=[2,,5,9].forEach(logArrayElements);//0 2 3
console.log(returnValue);undefined
//thisArg
function Counter(){
this.sum = 0;
this.count = 0;
}
Counter.prototype.add=function(array){
array.forEach(function(entry){
this.sum+=entry;
++this.count;
})
}
var obj=new Counter();
obj.add([2,5,9]);
console.log(obj.count);
//polyfill
if(!Array.prototype.forEach){
Array.prototype.forEach = function(callback, thisArg){
var T,K;
if(this == null){
throw new TypeError(' this is null or defined');
}
console.log(this);
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
if(typeof callback !== 'function'){
throw new TypeError(callback + 'is not a function');
}
if(arguments.length > 1){
T = thisArg;
}
K = 0;
while(K < len){
var kValue;
if(K in O){
callback.call(T,kValue,K,O);
}
K++;
}
}
}
every方法
every方法測試數(shù)組的所有元素是否都通過了指定函數(shù)的測試脆贵。
every為每個(gè)數(shù)組中的每個(gè)元素執(zhí)行一次callback函數(shù),直到它找到一個(gè)始callback返回false的元素起暮。如果發(fā)現(xiàn)了這樣的元素卖氨,every方法就會立即返回false,否則callback為每個(gè)元素返回true负懦,every方法就返回true筒捺。
回調(diào)函數(shù)參數(shù)同forEach。
function checkNumber(element,index,array){
return (element >= 10);
}
var passed=[1,11,5].every(checkNumber);
console.log(passed);//false
passed = [12, 54, 18, 130, 44].every(checkNumber);
console.log(passed);//true
//polyfill
if(!Array.prototype.every){
Array.prototype.every = function(fun,thisArg){
'use strict';
if(this === void 0 || this === null ){
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if( typeof fun !== 'function'){
throw new TypeError();
}
var thisArg = arguments.length >=2 ? arguments[1] : void 0;
for(var i = 0; i<len; i++ ){// in 操作符纸厉,當(dāng)對象為數(shù)組的時(shí)候 變量指的是數(shù)組的索引
if(i in t && !fun.call(thisArg,t[i],i,t)){
return false;
}
}
return true;
};
}
some方法
some 方法和every方法正好相反系吭,當(dāng)callback函數(shù)執(zhí)行返回true是,some方法立即返回true颗品。否則返回false肯尺。
實(shí)例同every類似。polyfill也和every類似躯枢,只是判斷情況返回的值和every正好相反则吟。
if (!Array.prototype.some){
Array.prototype.some = function(fun /*, thisArg */) {
'use strict';
if (this === void 0 || this === null) throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t && fun.call(thisArg, t[i], i, t)){//注意此處的判斷條件就好
return true;
}
}
return false;
};
}
map方法
map方法返回一個(gè)由元素組調(diào)用一個(gè)指定方法后的返回值組成的新數(shù)組。
需要注意的就是返回一個(gè)callback后的新數(shù)組锄蹂。
var numbers=[1,4,9]
var roots = numbers.map(Math.sqrt);
console.log(roots.join());//1,2,
map的其他實(shí)例
filter方法
filter方法使用指定的函數(shù)callback測試所有的元素逾滥,并創(chuàng)建一個(gè)包含所有通過測試的元素的新數(shù)組。
function isBigEnough( element ){
return element >= 10;
}
var filtered = [12,5,8,130,44].filter(isBigEnough);
console.log(filtered.join(':'));//12:130:44
結(jié)
數(shù)組遍歷的五個(gè)方法 <code>forEach</code>、<code>every</code>寨昙、<code>some</code>讥巡、<code>map</code><code>filter</code>。其中forEach舔哪、every欢顷、some不會改變原數(shù)組,而map捉蚤、filter方法會根據(jù)指定的函數(shù)callback創(chuàng)建一個(gè)新數(shù)組抬驴。filter將符合callback條件創(chuàng)建一個(gè)新數(shù)組,map創(chuàng)建對原素組每一項(xiàng)執(zhí)行callback后的數(shù)組缆巧。every對每一項(xiàng)都符合的返回true布持,some對只要有一項(xiàng)符合的就返回true,否則返回false陕悬。
學(xué)習(xí)中可能有許多不對题暖,請指出。