JavaScript Array(數組)對象


js 數組 - JavaScript Array(數組)對象

、 什麼是數組?

數組定義:存儲多個變量的容器;

<code>Var arr[值1,值2,值3]
元素:存在數組中的變量;
創建空數組:var=[ ]; var a=new array()/<code>

數組的基本方法

  1. concat()

:把一個數組和另一個數組拼接在一起 返回拼接好的數組

<code>1)     Var arr1=[1,2,3],
Arr2=[4,5,6],
Arr3=[7,8,9,1,2,3];
Console.log(arr1.concat(arr2,arr3));//[1,2,3,4,5,6,7,8,9,1,2,3]/<code>

2) 返回一個新數組,是將參數添加到原數組中構成的;

<code>Var a=[1,2,3,4,5];
Var b=a.concat(6,7);
document.write(a);//1,2,3,4,5
document.write(b);//1,2,3,4,5,6,7/<code>

2.join()

:把數組中的每一項 按照指定的分隔符拼接成字符串

<code>1) var arrs=[ ];
arrs[0]=”aa”;
arrs[1]=”bb”;
arrs[2]=”cc”;
console.log(arrs.join());  //aa,bb,cc未規定分隔符,默認“,”隔開;
console.log(arrs.join(“/”)); //aa/bb/cc
console.log(arrs.join(“-”)); //aa-bb-cc/<code>
<code>2) var a=[1,2,3,4,5];
var b=a.join(“|”);
document.write(a);//1,2,3,4,5
document.write(b);//1|2|3|4|5/<code>

3.pop()

:刪除數組的末尾項 返回值是刪除的數組項

<code>1)     var arrs=[1,2,3,4];
console.log(arrs.pop());4
console.log(arrs);//[1,2,3]/<code>
<code>2)     刪除原數組最後一項,並返回刪除元素的值;如果數組為空返回undefined
var a =[1,2,3,4,5];
var b= a.pop();
document.write(a);//1,2,3,4
document.write(b);//5/<code>

4.push()

向數組的末尾增加一項 返回值是數組的新長度

<code>1)     var arrs=[1,2,3];
arrs.push(4);
console.log(arrs);//[1,2,3,4]
arrs.push(5,6);
console.log(arrs);//[1,2,3,4,5,6]/<code>
<code>2)     將參數添加到原數組末尾,並返回數組的長度
var a=[1,2,3,4,5];
var b=a.push(6,7);
document.write(a);//1,2,3,4,5,6,7
document.write(b);//7/<code>

5.reverse()

:倒序數組 返回值倒序數組 原有數組改變

<code>1) var arrs=[1,2,3,4];
console.log(arrs.reverse());//[4,3,2,1]/<code>
<code>2) 將數組反序
var a=[1,2,3,4,5];
var b=a.reverse();
document.write(a);//5,4,3,2,1
document.write(b);//5,4,3,2,1/<code>

6.shift()

刪除數組開頭項 返回被刪除的開頭項目

<code>1)     var arr =[1,2,3,4];
console.log(arr.shift());//1
console.log(arrs);//[2,3,4];
var arr2=[ ];
console.log(arrs2.shift());//undefined
console.log(arrs2)//[]/<code>
<code>2)     var a =[1,2,3,4,5];
var b=a.shift();
document.write(a);//2,3,4,5
document.write(b);//1/<code>

7.sort()

根據匿名函數進行冒泡排序 b-a倒序 a-b升序

<code>1)     var arrs-[“tufenhua”,”longen”,”alibaba”];
console.log(arrs.sort());//[“alibaba”, ”longen”, “tufenhua”]/<code>
<code>2)     按指定的參數對數組進行排序
Var a=[9,1,7,3,5,4,6,2];
Var b=a.sort();
document.write(a);//1,2,3,4,5,6,7,9
document.write(b); //1,2,3,4,5,6,7,9/<code>

8.splice()

刪除數組中的任意項 返回值是被刪除的數組項

<code>1)     var arrs=[1,2,3,4,5,6];
 arr.splice(1,0,8); //先向數組的第二個位置中添加一個元素8
 
console.log(arrs);//[1,8,2,3,4,5,6]
arrs.splice(2,1);//從arrs數組中刪除一個元素2

console.log(arrs);//[1,8,3,4,5,6]
arrs.splice(1,1,10);//從arrs數組中替換一個元素8使他變為10
console.log(arrs);//[1,10,3,4,5,6]/<code>
<code>2)     從開始位置刪除,並從該位置開始添加
var a =[1,2,3,4,5,6,7,9];
var b=a.splice(2,2,1,8,9);//從第二個下標開始刪除兩個元素並添加1,8,9
var b=a.splice(0,1);//同shift,在開始下標,刪除一個元素1
a.splice(0,0,-2,-1);//同unshift 在開始下標添加刪除0個元素並添加-2-1
var b=a.length;
var b=a.splice(a.length-1,1);//同pop,刪除最後一個元素9/<code>

9.splice(a.length,0,6,7);//同push,末尾添加6,7

<code>var b=a.length;
document.write(a);//-2,-1,2,1,8,9,5,6,7,6,7
document.write(b);//11/<code>

10.toString()

方法可把一個邏輯值轉換為字符串,並返回結果。

<code>1)     var arrs=[1,2];
console.log(arrs.toString());//1,2/<code>
<code>2)     var a=[1,2];
document.write(a.toString());//1,2/<code>

11.unshift

向數組開頭增加一項 返回值是數組的新長度

<code>1)     var arrs =[1,2];
arrs.unshift(3);
console.log(arrs);//[3,1,2]/<code>
<code>2)     var a=[1,2,3,4,5];
var b=a.unshift(-2,-1);
document.write(a);//-2,-1,1,2,3,4,5
document.write(b);//7/<code>

12.slice()

複製數組 返回值是複製到的新數組 寫上數值之後 不包含被複制的最後一項

<code>1)     var arrs2=[1,2,3];
console.log(arrs2.slice(1));//[2,3] 截取1後面的元素
console.log(arrs2.slice(0,1));//[1] 截取0到1之間的元素值
console.log(arrs2.slice(1,-1));//[2] 截取1到-1之間的元素值
console.log(arr2.slice(2,-1));//[]   截取2到-1之間的元素值
console.log(arr2.slice(0));//[1,2,3]  截取所有元素值/<code>
<code>2)var a =[1,2,3,4,5,6];
var b= a.slice(2,5);
document.write(a);//1,2,3,4,5,6
document.write(b);//3,4,5/<code>

二、創建數組

<code>Var arr=[ ]//空數組
Var arr1=[1,2,3,4,5,6];
Var arr2=[1,2,3,true,flase ,null,undefined,[1,2,3]{x:1,y:2}];/<code>

通過Array()創建數組

<code>Var a=new array()
Var a=new array(5);
Console.log(a length);                            結果:5/<code>

三、ES5新增

1.index of 和last index of

  • indexOf() 方法可返回某個指定的字符串值在字符串中首次出現的位置。
  • lastIndexOf() 方法可返回一個指定的字符串值最後出現的位置,在一個字符串中的指定位置從後向前搜索。
<code>   Var str=”hello world”;
  Console.log(str.indexof(“o”));//4
  Console.log(str.lastindexof(“o”));//7
  Console.log(str.lastindexof(“hello”));//-1
  Console.log(str.indexof(“o”,6));//7
  Console.log(str.lastindexof(“o”,6));//4/<code>

2.foreach():*foreach方法中的function回調有三個參數:第一個參數是遍歷數組的內容,第二參數是對應的數組索引,第三個參數是數組本身

*用來遍歷數組中的每一項,這個方法執行是沒有返回值得,對原來數組也沒有影響;

*數組中有幾項,傳遞的匿名回調函數就需要執行幾次

*每一次執行匿名函數的時候,還傳遞三個參數值,數組中當前項item,當前項的 索引index,原始數組input;

<code> 實例:forEach方法中的this 是ary,匿名回調函數中的this默認是window
var ary =[12,23,24,42,1];
Var res=ary.forEach(function(item,index,input)){
Input[index]=item*10;
})
Console.log(res);//undefined
Console.log(ary);//120,230,240,420,10會對原來的數組產生改變;./<code>

3,map:map的回調函數中支持return返回值;返回值,相當於把原數組克隆一份,把克隆的這一份數組的對應項改變了,

<code>實例:通過指定函數處理數組的每個元素,並返回處理後的數組
var arr=[12,23,24,42,1];
      Var res=arr.map(function(item,index,input)){
       Return item*10;
     })
     Console.log(res);// 120,230,240,420,10原數組拷貝了一份,並進行了修改
     Console.log(ary);// 12,23,24,42,1原數組未發生改變/<code>

4.filter( )檢測數組元素,並返回符合條件所有元素的數組

<code> 實例:把符合條件的項目組成一個新的數組
     Var checknum=[15,3,2,6,7,1,9,10];
     Var checkresult=checknum.filter(function(item,index,array)){
  Return item >3;
  });
Checkresult.forEach(function(value,index,array)){
    Console.log(value);
})                            結果:15,6,7,9,10/<code>

5.every()檢測數組元素的每個元素是否都符合條件

<code> 實例:數值中的元素全部滿足指定條件返回ture
  Var checknum=[15,3,2,6,7,1,9,10];
   Var checkresult=checknum.every(function(item,index,array)){
      Return item>1;
  });
alert(checkresult);       結果:false/<code>

6.some()檢測數組元素中是否有元素符合指定條件

<code>  實例:數組中的部分滿足指定條件返回true
Var checknum=[15,3,2,6,7,1,9,10];
   Var checkresult=checknum.every(function(item,index,array)){
      Return item>15;
  });
 alert(checkresult);       結果:false/<code>

7.reduce()和reduceRight()

1) Reduce()方法接收一個函數callbackfn作為累加器(accumulator)數組中的每個值(從左到右)開始合併,最終為一個值

語法:array.reduce(callbackfn,[initiaValue])

Reduce()方法接收callbackfn函數,這個函數包含四個參數

Function callbackfn(preValue,curValue,index,array){}

preValue:上一次調用回調返回的值,或者是提供的初始值(intialValue)

curValue:數組中當前被處理的數組項

index:當前數組項在數組中的索引值

array:調用reduce()方法的數組

initialValue作為第一次調用callbackfn函數的第一個參數

注:reduce()方法為數組中的每一個元素一次執行回調函數callbackfn,不包括數組中被刪除或從未被賦值的元素,接受四個參數:初始值(或者上一次回調函數的返回值),當前元素值,當前索引,調用reduce()數組。

回調函數第一次執行時,preValue和curValue可以是一個值,如果initialValue在調用reduce()時被提供,第一個preValue等於inialValue,並且curValue等於數組中的第一個值;如果未被提供,那麼preValue等於數組中的第一個值,curValue等於數組中的第二個值。

<code>Var arr=[0,1,2,3,4];
Arr.reduce(function (preValue,curValue,index,array){
  Return preValue+curValue;
});//10/<code>

實例中回調函數被執行四次,

js 數組 - JavaScript Array(數組)對象

reduceRight()方法

同reduce相同,不同的是,reduceRight()從數組的末尾向前將數組中的數組項做累加

<code>   Var arr=[0,1,2,3,4];
Arr.reduceRight(function (preValue,curValue,index,array){
  Return preValue+curValue;
});//10    /<code>
js 數組 - JavaScript Array(數組)對象

【根據自己的理解手敲,如有錯誤,請留言指正,待大神指點】...


分享到:


相關文章: