數(shù)組遍厲的5種方式 : forEach( ) , map( ) , filter( ) , some( ) , every( )
1. forEach ( callback( item , index , array ) ) : 對(duì)數(shù)組元素執(zhí)行一次指定的回調(diào)函數(shù)死讹,無(wú)返回?cái)?shù)據(jù)
arguments : item , index , array
item:遍厲的每個(gè)數(shù)組內(nèi)容
index: 對(duì)應(yīng)數(shù)組內(nèi)容的索引值
array: 遍歷的數(shù)組本身
*****
var arr = [1,3,5,7 ];
arr.forEach( function( item , index , array ) {
console.log( item ); // 1 , 3 , 5 , 7
console.log( index ); // 0 , 1 , 2 , 3
console.log( array ); // [ 1 , 3 , 5 , 7 ]
} )
2. filter ( callback( item , index , array ) , [ thisArgs ] ) : 對(duì)數(shù)組元素執(zhí)行一次指定的回調(diào)函數(shù)百侧,返回一個(gè)新數(shù)組( 滿足篩選條件的數(shù)據(jù)),原數(shù)組不變
arguments: item , index , array 趋厉,thisArgs
item: 遍歷的每個(gè)數(shù)據(jù)
index:對(duì)應(yīng)數(shù)據(jù)的索引值
array: 遍歷的數(shù)組本身
thisArgs : (可選)回調(diào)函數(shù)this指向 , 如果 thisArg 參數(shù)有值眉睹,則每次 callback 函數(shù)被調(diào)用的時(shí)候郭计,this 都會(huì)
指向 thisArg 參數(shù)上的這個(gè)對(duì)象涝登。如果省略了 thisArg 參數(shù),或者賦值為 null 或 undefined,則 this 指向全局對(duì)象 氮双。
****
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present"];
var filterWords = words.filter( function( item , index , array ) {
return item.length > 5 ;
//item: "spray", "limit", "elite", "exuberant", "destruction", "present"
//index: 0 , 1 , 2 , 3 , 4 , 5
//array ["spray", "limit", "elite", "exuberant", "destruction", "present"]
//filterWords: [ 'exuberant', 'destruction', 'present' ]
} )
3.map ( callback( item , index , array ) , [ thisArgs ] ) : 對(duì)數(shù)組元素執(zhí)行一次回調(diào)函數(shù) 碰酝,返回一個(gè)新數(shù)組 ( 執(zhí)行過(guò)回調(diào)的數(shù)據(jù)), 原數(shù)組不變
arguments: item , index , array 眶蕉,thisArgs
item: 遍歷的每個(gè)數(shù)據(jù)
index:對(duì)應(yīng)數(shù)據(jù)的索引值
array: 遍歷的數(shù)組本身
thisArgs : (可選)回調(diào)函數(shù)this指向 , 如果 thisArg 參數(shù)有值砰粹,則每次 callback 函數(shù)被調(diào)用的時(shí)候,this 都會(huì)
指向 thisArg 參數(shù)上的這個(gè)對(duì)象造挽。如果省略了 thisArg 參數(shù),或者賦值為 null 或 undefined碱璃,則 this 指向全局對(duì)象 。
****
var words = [ 1 , 2 , 4 , 6 ];
var filterWords = words.map( function( item , index , array ) {
return item + 2;
//item: 1 , 2 , 4 , 6
//index: 0 , 1 , 2 , 3
//array: [ 1 , 2 , 4 , 6 ]
//filterWords: [ 3 , 4 , 6 , 8 ]
} )
ps : 與filter區(qū)別是執(zhí)行判斷語(yǔ)句返回的不是滿足條件的數(shù)據(jù)結(jié)果饭入,而是一個(gè)true/false的數(shù)組 嵌器,即:
var words = [ 1 , 2 , 4 , 6 ];
var mapWords = words.map( function( item , index , array ) {
return item > 3; //mapWords: [ false , false , true , true ]
} )
var filterWords = words.filter( function( item , index , array ) {
return item > 3; //filterWords: [ 4 , 6 ];
} )
4.some( callback( item,index,array ) ,[ thisArgs ]): 執(zhí)行指定的回調(diào)函數(shù),只要數(shù)組種有一個(gè)滿足條件的就返回true谐丢,否則返回false
[ 1 , 4 , 8 , 5 ].some( function(item){return item > 10} ) //false
[ 1 , 4 , 8 , 5 ].some( function(item){return item > 5;}) //true
5.every( callback( item,index,array) ): 執(zhí)行指定的回調(diào)爽航,只有所有的數(shù)據(jù)滿足條件才返回true,否則返回false
var evenNum = function( num ){
if(typeof num !== 'number'){
return false
}
if( num % 2 !== 0 ){
return false
}
return true
}
[ 2 , 4 , 6 , 8 , 9 ].every( evenNum ) //false
[ 2 , 4 , 6 , 8 , 10 ].every( evenNum ) //true