JavaScript-Array 對象及方法(上)

1. 創建數組對象的方式

1. 數組字面量

js中的數組對象可以通過構造函數Array()來創建,但同時也可以用字面量表示,且更簡單更可取。

var a = ["Ary","Snow","Sansa"];
console.log(typeof a);//輸出"object",這是由於數組本身也是對象類型
console.log(a.constructor===Array);//true

2. 數組構造函數

  • 先聲明,然後再賦值:
var a = new Array();
a[0]="Ary";
a[1]="Snow";
a[2]="Sansa";
  • 直接在創建時賦值:
var a = new Array("Ary","Snow","Sansa");
  • 注意:
    當向Array()構造函數傳入單個數字時,它並不會成爲數組的第一個元素的值,反而會設置數組長度。
//長度爲3的數組
var a = new Array(3);
console.log(a.length);//3
console.log(a[0]);//undefined,這是因爲只限定了數組長度,而數組中並不存在任何元素值

//具有一個元素的數組
var a =[3];
console.log(a.length);//1
console.log(a[0]);//3

//如果該值爲數值,但小於0或不爲整數,發生運行時錯誤。
var a = new Array(3.12);//Uncaught RangeError: Invalid array length(錯誤範圍,不合法的數組長度)

console.log(a.length);//Cannot read property 'length' of undefined
console.log(a[0]);//Cannot read property '0' of undefined

console.log(typeof a);//輸出"undefined"

2. 數組對象的屬性

constructor 屬性 | length 屬性 | prototype 屬性

1. constructor屬性

對象的constructor 屬性用於返回創建該對象的函數,也就是我們常說的構造函數。

// 數組:Array()
var arr = [1, 2, 3];
document.writeln(arr.constructor); // function Array() { [native code] }
document.writeln(arr.constructor === Array); // true

2. length 屬性

因爲一個數組中的元素並不一定是連續的,所以 length 屬性也並不一定就等於數組中的元素個數。例如:

var my_array = new Array( );
my_array[0] = "Test";
my_array[6] = "Another Test";
my_array.length;//7
  • 如果 length 屬性被賦予了一個比原先值小的數值,那麼數組就被截斷,所有數組下標等於或者大於 length 屬性的新值的元素都會被丟失。
  • 如果 length 屬性被賦予了一個比原先值大的數值,那麼數組就被擴展,且所有新建元素都被賦值爲 undefined。

3. prototype 屬性

返回對象類型 原型 的引用。
Array.prototype.max = array_max;

3. 數組對象的方法

1. join 方法

返回字符串值,其中包含了連接到一起的數組的所有元素,元素由指定的分隔符分隔開來。
arr.join(“”)

 var a, b,c;   
 a = [0,1,2,3,4,'10','js'];
 b = a.join("-");
c=a.join("");
console.log(b);//0-1-2-3-4-10-js
console.log(c);//0123410js
  • split()方法:用於把一個字符串分割成字符串數組. 與join相對應,但顯然並不是數組對象的方法
    str.split(a,num);
    1. 以a爲分割點,必填
    2. num返回的新的數組的長度
var str,str1,str2;
str = "這是一段話_很好的一段話_!";
str1=str.split("_",2);//["這是一段話", "很好的一段話"]
str2=str.split("");//["這", "是", "一", "段", "話", "_", "很", "好", "的", "一", "段", "話", "_", "!"]

2. push、pop、shift、unshift 方法

方法 語法 參數 作用
push arrj.push(ele1,ele2,…,eleX) 該 Array 的新元素,可選 向數組的尾部添加一個或多個元素,並返回新的長度
unshift arrj.unshift (ele1,ele2,…,eleX) 該 Array 的新元素,可選 向數組的頭部添加一個或多個元素,並返回新的長度
pop arr.pop() 無,填入數值無作用 pop() 方法用於刪除並返回數組的最後一個元素。數組長度發生變化
shift arr.shift() 無,填入數值無作用 shift() 方法用於把數組的第一個元素從其中刪除,並返回第一個元素的值。數組長度發生變化

**注意:**1、以上方法是直接對原數組進行的操作。2、使用pop,shift方法時,如果原數組爲空,那麼將返回 undefined。
3、個人爲避免記錯:(這四個方法,含有字母u的,都是添加操作,有參數;含有字母p的,都作用於數組尾部)

例:

var arr=['a','b','c','d'],arr0=[];
arrPush=arr;
for(var i=0;i<3;i++){
arr.push(i);
}
arr0.pop();     //undefined
console.log(arr);//["a", "b", "c", "d", 0, 1, 2]

arr.unshift("head");    //8
console.log(arr)//["head", "a", "b", "c", "d", 0, 1, 2]
arr.shift()     //head
arr.shift()     //"a"
arr.pop()       //2

3. concat 方法

返回一個新數組,這個新數組是由兩個或更多數組組合而成的。
arrayObject.concat(array1,array2,…,arrayN)

本方法在使用時,會進行兩個操作:連接,複製

例:

var arr=[1,2,3];
console.log(arr.concat(4,5,"6"));//[1, 2, 3, 4, 5, "6"]
console.log(arr.concat(4,5,"6").join(""));//123456
console.log(arr.concat(arr,4,5,"6"));//[1, 2, 3, 1, 2, 3, 4, 5, "6"]

function ConcatArrayDemo(){ 
  var a, b, c, d;   
  a = new Array(1,2,3);
  b = "Hello";  
  c = new Array(42, "JavaScript");
  d = a.concat(b, c);
  return(d);
}
ConcatArrayDemo();//[1, 2, 3, "Hello", 42, "JavaScript"]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章