1.定義數組
var m=new Array();
var n=[];
2.數組的賦值(兩種)
A. var m=new Array(2); 一個值表示數組length
var m=new Array(2,3,4,5); 多個值表示數組賦值
B. m[0]=2; m[1]=3; m[2]=4;
3.數組對象的屬性
數組名稱.length;
4.數組對象的常用方法
數組轉化為string join()
var m=new Array(2,3,4,5);
console.log(m.join("|"));
結果: 2|3|4|5
數組翻轉 reverse()
var m=new Array(2,3,4,5);
console.log(m.reverse());
結果:5 4 3 2
數組的截斷 slice()
slice(start.index, end.index); 對原數組沒有影響 索引位不取大
var m=new Array(2,3,4,5);
console.log(m.slice(0, 3),m);
結果:(2,3,4) (2,3,4,5)
數組的截斷 splice()
splice(start.index , length); 對原數組有影響
var m=new Array(2,3,4,5);
console.log(m.splice(1, 3),m);
結果:(3,4,5) (2)
數組增加元素 push() 后
var m=new Array(2,3,4,5); 返回length
console.log((m.push(6)),m);
結果: 5 (2,3,4,5,6)
數組增加元素 unshift() 前
var m=new Array(2,3,4,5);
console.log(m.unshift(6 ,23),m);
結果: 6 (6,23,2 ,3,4,5)
數組刪除元素 pop() 后
var m=new Array(2,3,4,1);
console.log(m.pop(),m); 返回刪除值
結果:1 ,(2,3,4)
數組刪除元素 shift() 前
var m=new Array(2,3,4,1);
console.log(m.shift(),m);
結果: 2 ,(3,4,1)