js數組方法的總結

一、隊尾插入pushjavascript

var colors = ["red","green"];
   colors.push("black"):
   console.log(colors); //["red","green","black"]

二、隊尾刪除並返回刪除的最後一項popjava

var colors = ["red","green","black"];
   var item = colors.pop();
   console.log(item); //"black"

三、隊首插入unshift()數組

var colors = ["red","green"];
   colors.unshift("black");
   var item = colors.pop();
   console.log(item); //"black"

四、隊首刪除shift()app

var colors = ["red","green","black"];
   colors.shift();
   console.log(colors); //["green","black"]

五、數組一添加數組二concat()函數

var colors = ["red","green","black"];
   var colors2 = colors.concat("yellow",["blue","brown"]);
   console.log(colors); //["red","green","black"]
   console.log(colors2); //["red","green","black","yellow","blue","brown"]

六、數組的截取slice()
只傳一個參數:從數組這個參數的下標開始截取一直到數組結束。code

var colors = ["red","green","black"];
   colors.slice(1); //["green","black"]
   console.log(colors); //["red","green","black"]

傳兩個參數:第一個是截取開始的位置,第二個是截取結束的位置對象

var colors = ["red","green","black","yellow","blue","brown"];
   colors.slice(1,3)//從位置1開始,到位置2結束["green","black"];

七、數組的splice()方法ip

有三種用法:
  • 刪除:能夠刪除任意數量的項,只需指定兩個參數:第一個參數爲刪除開始的位置,第二個參數爲刪除項數。
  • 插入:能夠向指定位置插入任意數量的項,只需提供3個參數:起始位置、0(要刪除的項數)和要插入的項。例如:splice(2,0,"red","green"),會從當前數組的位置2開始插入字符串"red"和"green"。
  • 替換: 能夠向指定位置插入任意數量的項,且同事刪除任意數量的項,只需提供3個參數:起始位置、要刪除的項數和要插入的任意數量的項。插入的項數沒必要與刪除項數相等。例如:splice (2,1,"red","green") 會刪除當前數組位置2的項,而後再從位置2開始插入字符串。
var colors = ["red","green","black"];
   var removed = colors.splice(0,1);
   console.log(colors); //["green","black"]
   console.log(removed); //["red"]
    
   removed = colors.splice(1,0,"yellow","orange");
   console.log(colors); //["green","yellow","orange","black"]
   console.log(removed); //[]

   removed = colors.splice(1,1,"red","purple");
   console.log(colors); //["green","red","purple","orange","black"]
   console.log(removed); //["yellow"]

八、位置方法indexOf()和lastIndexOf()rem

indexOf()和lastIndexOf()都接收兩個參數,第一個參數是要查找的項,第二個(可選)查找開始的位置,indexOf()是從數組頭開始查,lastIndexOf()是從數組尾開始查找。
var numbers = [1,2,3,4,5,4,3,2,1];

   console.log(numbers.indexOf(4)); //3
   console.log(numbers.lastIndexOf(4)); //5
   
   console.log(numbers.indexOf(4,4)); //5
   
   var person = {name: "vivi"};
   var people = [{name: "vivi"}];
   
   var morePeople = [person];
   console.log(people.indexOf(person)); //-1
   console.log(morePeople.indexOf(person)); //0

九、查找find()方法
查找符合條件的第一項字符串

var inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}
   ];
   const inventorItem = inventory.find((item) => item.name === 'apples');
   console.log(inventorItem); //{name: 'apples', quantity: 2}

十、迭代方法
傳入這些方法中的函數會接收三個參數:數組項的值、該項在數組中的位置和數組對象自己。

  • every(): 對數組中的每一項運行給定函數,若是該函數對每一項都返回true,則返回true。
  • filter(): 對數組中的每一項運行給定函數,返回該函數會返回true的項組成的數組。
  • forEach(): 對數組中的每一項運行給定函數,這個方法沒有返回值。
  • map(): 對數組中的每一項運行給定函數,返回每次函數調用的結果組成的數組。
  • some(): 對數組中的每一項運行給定函數,若是該函數對任一項返回true,則返回true。
var numbers = [1,2,3,4,5,4,3,2,1];
   var everyResult = numbers.every((item, index, array) => {
        return item > 2;
   });
   console.log(everyResult); //false
   
   var someResult = numbers.some((item, index, array) => {
       return  item > 2;
   });
   console.log(someResult); //true
var numbers = [1,2,3,4,5,4,3,2,1];
  var filterResult = numbers.filter((item, index, array) => {
    return item > 2;
  });
  console.log(filterResult); //[3,4,5,4,3]
var numbers = [1,2,3,4,5,4,3,2,1];
   var mapResult = numbers.map((item, index, array) => {
    return item * 2;
  });
  console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
var numbers = [1,2,3,4,5,4,3,2,1];
   numbers.forEach((item, index, array) => {
       //執行某些操做
   });

十一、歸併方法reduce()和reduceRight()

這兩個方法都會迭代數組的全部項,而後構建一個最終返回的值。
reduce()方法從數組的第一項開始,逐個遍歷到最後。                                  
reduceRight()則從數組的最後一項開始,向前遍歷到第一項。
var values = [1,2,3,4,5];
   var sum = values.reduce((prev, cur, index, array) => {
       return prev + cur;
   });
   console.log(sum); //15
var values = [1,2,3,4,5];
   var sum = values.reduceRight((prev, cur, index, array) => 
   {
       return prev + cur;
   });
   console.log(sum); //15
相關文章
相關標籤/搜索