1. 程式人生 > >Array - 最常用的js物件之一

Array - 最常用的js物件之一

陣列物件: Array

1. 前言

陣列也好, 列表也好, 在不同的程式語言裡面叫法雖然不一樣, 但其本質是一樣的, 作為最常用的物件, 其屬性和方法是必須要牢記在心的, 以下是Array物件的完整屬性方法示例.

2. 引數

情況1: 沒引數

var arr = new Array(); // 例項化時括號中不加引數, 返回一個空陣列
console.log(arr); // 輸出: []

情況2: 有數字引數

var arr = new Array(5); // 括號中的10代表例項化的陣列中的元素個數;
console.log(arr); // 輸出: [null,null,null,null,null]
console.log(arr.length); // 輸出: 5

情況3: 有一系列資料引數

var arr = new Array('A',"B",10,[1,2,3]);
console.log(arr); // 輸出: ['A',"B",10,[1,2,3]]
console.log(arr.length); // 輸出: 4

3. 屬性

constructor: 返回對建立此物件的陣列函式的引用

var arr = new Array();

console.log(arr.constructor); // 返回: [object Function]
document.write(arr.constructor); // 返回: function Array() { [native code] }

length: 返回陣列的長度

var arr = new Array(1,2,3);

console.log(arr.length); // 3

prototype: 有能力向物件新增屬性和方法

var arr = new Array();

Array.prototype.name = "雷棠";
console.log(arr.name); // 雷棠;

4. 方法

Array.concat(): 陣列拼接

var arr = new Array("hello");

console.log(arr.concat(["world"])); // ["hello", "world"]
console.log(arr.concat("world","!")) // ["hello","world","!"]

Array.join(): 陣列轉字串

var arr = new Array(1,2,3,4);

console.log(arr); // [1,2,3,4]
console.log(arr.join()); // '1,2,3,4'
console.log(arr.join("-")); // '1-2-3-4'

Array.pop(): 刪除陣列的末尾元素

var arr = new Array("A");
console.log(arr.pop()); // 'A'
console.log(arr.length); // 0

console.log(arr.pop()); // undefined
console.log(arr.length); // 0

Array.push(): 往陣列末尾新增元素

var arr = new Array("A");
console.log(arr); // "A"
console.log(arr.length); // 1

console.log(arr.push("B","C","D")); // 4
console.log(arr); // ["A","B","C","D"]
console.log(arr.length); // 4

Array.reverse(): 倒序陣列

var arr = new Array("A","B","C");
console.log(arr.reverse()); // ["C","B","A"]
console.log(arr); // ["C","B","A"]

Array.shift(): 刪除陣列的首個元素

var arr = new Array("A","B");
console.log(arr.shift()); // "A"
console.log(arr); // ["B"]
console.log(arr.shift()); // "B"
console.log(arr); // []
console.log(arr.shift()); // undefined
console.log(arr); // []

Array.slice(): 從已有陣列中返回選定的元素

var arr = new Array("A","B","C","D","E");

console.log(arr.slice()); // ["A","B","C","D","E"]
console.log(arr.slice(1)); // ["B","C","D","E"]
console.log(arr.slice(1,3)); // ["B","C"]
console.log(arr.slice(end=3)); // ["D","E"]
console.log(arr.slice(-1,-2)); // []
console.log(arr.slice(-2,-1)); // ["D"]
console.log(arr); // ["A","B","C","D","E"]

Array.sort(): 陣列元素排序

var arr1 = new Array(1,3,2,4,6,5);
console.log(arr1.sort()); // [1,2,3,4,5,6]
console.log(arr1); // [1,2,3,4,5,6]

var arr2 = new Array("1","3","2","4","6","5");
console.log(arr2.sort()); // ["1","2","3","4","5","6"]
console.log(arr2); // ["1","2","3","4","5","6"]

var arr3 = new Array("B","d","A","c","C","D");
console.log(arr3.sort()); // ["A","B","C","D","c","d"]
console.log(arr3); // ["A","B","C","D","c","d"]

Array.splice(): 刪除並新增新元素

var arr = new Array("A","B","C");
console.log(arr.splice(2,1)); // ["C"]
console.log(arr); // ["A","B"]
console.log(arr.splice(1,0,"C")); // []
console.log(arr); // ["A","C","B"]

Array.toSource(): 返回物件原始碼

// 注意: 只有gecko核心的瀏覽器支援此方法
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.toSource()); // ({name:"Bill Gates", job:"Engineer", born:1985}) 

Array.toString(): 陣列轉字串並返回結果

var arr = new Array(1,2,3,4);
console.log(arr.toString()); // "1,2,3,4"

Array.toLocalString(): 轉換為本地陣列並返回

var arr = new Array(1,2,3,4);
console.log(arr.toLocaleString()); // "1,2,3,4"

Array.unshift(): 向陣列開頭新增一個或多個數組並返回新的長度

// 注意: 在IE中無法正常工作
var arr = new Array(0,1,2);
console.log(arr.unshift(-1,-2)); // 5
console.log(arr); // [-1,-2,0,1,2]
console.log(arr.unshift()); // 5

Array.valueOf(): 返回陣列物件原始值

var arr1 = new Array(1,2,3);
console.log(arr1.valueOf()); // [1,2,3]

5. 結論

OK, 這是目前收集到的所有Array物件的屬性和方法的使用示例, 都是非常基礎和簡單的, 在初期應該會經常翻閱.