欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

5個(gè)數(shù)組Array方法: indexOf、filter、forEach、map、reduce使用實(shí)例

 更新時(shí)間:2015年01月29日 10:09:55   投稿:junjie  
這篇文章主要介紹了5個(gè)數(shù)組Array方法: indexOf、filter、forEach、map、reduce使用實(shí)例,需要的朋友可以參考下

ECMAScript5標(biāo)準(zhǔn)發(fā)布于2009年12月3日,它帶來(lái)了一些新的,改善現(xiàn)有的Array數(shù)組操作的方法。然而,這些新奇的數(shù)組方法并沒(méi)有真正流行起來(lái)的,因?yàn)楫?dāng)時(shí)市場(chǎng)上缺乏支持ES5的瀏覽器。

Array "Extras"

沒(méi)有人懷疑這些方法的實(shí)用性,但寫polyfill(PS:兼容舊版瀏覽器的插件)對(duì)他們來(lái)說(shuō)是不值得的。它把“必須實(shí)現(xiàn)”變成了“最好實(shí)現(xiàn)”。有人居然將這些數(shù)組方法稱之為Array "Extras"。哎!

但是,時(shí)代在變化。如果你看看Github上流行的開源JS項(xiàng)目,你會(huì)發(fā)現(xiàn)趨勢(shì)正在轉(zhuǎn)變。大家都想削減大量(第三方庫(kù))的依賴,僅用本地代碼來(lái)實(shí)現(xiàn)。

好了,讓我們開始吧。

我的5個(gè)數(shù)組

在ES5中,一共有9個(gè)Array方法 http://kangax.github.io/compat-table/es5/

注* 九個(gè)方法

Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.every
Array.prototype.some
Array.prototype.forEach
Array.prototype.map
Array.prototype.filter
Array.prototype.reduce
Array.prototype.reduceRight

我將挑選5種方法,我個(gè)人認(rèn)為是最有用的,很多開發(fā)者都會(huì)碰到。

1) indexOf

indexOf()方法返回在該數(shù)組中第一個(gè)找到的元素位置,如果它不存在則返回-1。

不使用indexOf時(shí)

var arr = ['apple','orange','pear'],
found = false;

for(var i= 0, l = arr.length; i< l; i++){
if(arr[i] === 'orange'){
found = true;
}
}

console.log("found:",found);

使用后

var arr = ['apple','orange','pear'];

console.log("found:", arr.indexOf("orange") != -1);

2) filter

該filter()方法創(chuàng)建一個(gè)新的匹配過(guò)濾條件的數(shù)組。

不用 filter() 時(shí)

var arr = [
  {"name":"apple", "count": 2},
  {"name":"orange", "count": 5},
  {"name":"pear", "count": 3},
  {"name":"orange", "count": 16},
];
  
var newArr = [];

for(var i= 0, l = arr.length; i< l; i++){
  if(arr[i].name === "orange" ){
newArr.push(arr[i]);
}
}

console.log("Filter results:",newArr);


用了 filter():

var arr = [
  {"name":"apple", "count": 2},
  {"name":"orange", "count": 5},
  {"name":"pear", "count": 3},
  {"name":"orange", "count": 16},
];
  
var newArr = arr.filter(function(item){
  return item.name === "orange";
});


console.log("Filter results:",newArr);

3) forEach()

forEach為每個(gè)元素執(zhí)行對(duì)應(yīng)的方法

var arr = [1,2,3,4,5,6,7,8];

// Uses the usual "for" loop to iterate
for(var i= 0, l = arr.length; i< l; i++){
console.log(arr[i]);
}

console.log("========================");

//Uses forEach to iterate
arr.forEach(function(item,index){
console.log(item);
});

forEach是用來(lái)替換for循環(huán)的

4) map()

map()對(duì)數(shù)組的每個(gè)元素進(jìn)行一定操作(映射)后,會(huì)返回一個(gè)新的數(shù)組,

不使用map

var oldArr = [{first_name:"Colin",last_name:"Toh"},{first_name:"Addy",last_name:"Osmani"},{first_name:"Yehuda",last_name:"Katz"}];

function getNewArr(){
  
  var newArr = [];
  
  for(var i= 0, l = oldArr.length; i< l; i++){
    var item = oldArr[i];
    item.full_name = [item.first_name,item.last_name].join(" ");
    newArr[i] = item;
  }
  
  return newArr;
}

console.log(getNewArr());

使用map后

var oldArr = [{first_name:"Colin",last_name:"Toh"},{first_name:"Addy",last_name:"Osmani"},{first_name:"Yehuda",last_name:"Katz"}];

function getNewArr(){
    
  return oldArr.map(function(item,index){
    item.full_name = [item.first_name,item.last_name].join(" ");
    return item;
  });
  
}

console.log(getNewArr());


map()是處理服務(wù)器返回?cái)?shù)據(jù)時(shí)是一個(gè)非常實(shí)用的函數(shù)。

5) reduce()

reduce()可以實(shí)現(xiàn)一個(gè)累加器的功能,將數(shù)組的每個(gè)值(從左到右)將其降低到一個(gè)值。

說(shuō)實(shí)話剛開始理解這句話有點(diǎn)難度,它太抽象了。

場(chǎng)景: 統(tǒng)計(jì)一個(gè)數(shù)組中有多少個(gè)不重復(fù)的單詞

不使用reduce時(shí)

var arr = ["apple","orange","apple","orange","pear","orange"];

function getWordCnt(){
  var obj = {};
  
  for(var i= 0, l = arr.length; i< l; i++){
    var item = arr[i];
    obj[item] = (obj[item] +1 ) || 1;
  }
  
  return obj;
}

console.log(getWordCnt());

使用reduce()后

var arr = ["apple","orange","apple","orange","pear","orange"];

function getWordCnt(){
  return arr.reduce(function(prev,next){
    prev[next] = (prev[next] + 1) || 1;
    return prev;
  },{});
}

console.log(getWordCnt());

讓我先解釋一下我自己對(duì)reduce的理解。reduce(callback, initialValue)會(huì)傳入兩個(gè)變量?;卣{(diào)函數(shù)(callback)和初始值(initialValue)。假設(shè)函數(shù)它有個(gè)傳入?yún)?shù),prev和next,index和array。prev和next你是必須要了解的。

一般來(lái)講prev是從數(shù)組中第一個(gè)元素開始的,next是第二個(gè)元素。但是當(dāng)你傳入初始值(initialValue)后,第一個(gè)prev將是initivalValue,next將是數(shù)組中的第一個(gè)元素。

比如:

/*
* 二者的區(qū)別,在console中運(yùn)行一下即可知曉
*/

var arr = ["apple","orange"];

function noPassValue(){
  return arr.reduce(function(prev,next){
    console.log("prev:",prev);
    console.log("next:",next);
    
    return prev + " " +next;
  });
}
function passValue(){
  return arr.reduce(function(prev,next){
    console.log("prev:",prev);
    console.log("next:",next);
    
    prev[next] = 1;
    return prev;
  },{});
}

console.log("No Additional parameter:",noPassValue());
console.log("----------------");
console.log("With {} as an additional parameter:",passValue());

相關(guān)文章

最新評(píng)論