屬性名 | 描述 |
---|---|
prototype | 爲對象添加屬性、方法 |
constructor | 返回數組對象引用 |
length | 返回數組元素數目 |
方法名 | 描述 | 返回 | 更改原數組 |
---|---|---|---|
concat() | 鏈接多個數組 | 鏈接後新數組 | N |
join('') | 將數組中全部元素合爲一個字符串。按分隔符劃分 | 合併後新數組 | N |
toString() | 數組轉換爲字符串( 與無參join相同,逗號鏈接) | 轉換後字符串 | N |
pop() | 刪除數組最後一個元素(棧頂) | 刪除的元素值 | Y |
push() | 向數組的末尾添加一個/多個元素 | 新數組長度 | Y |
shift() | 刪除數組第一個元素 | 刪除的元素值 | Y |
unshift() | 向數組的開頭添加一個/多個元素 | 新數組長度 | Y |
reverse() | 顛倒數組中元素的順序 | 倒序後數組 | Y |
slice(start,end) | 截取從start到end子數組(end省略爲數組末尾) | 截取子數組 | N |
splice() | (start,length,item1,item2,...)刪除元素並添加新元素 | 刪除子數組 | Y |
sort() | 對數組的元素進行排序(可自定規律) | 排序後數組 | Y |
valueOf() | 返回 Array 對象的原始值 | Array對象 | N |
indexOf() | 返回元素在數組中首次出現的位置(嚴格匹配===) | 下標/-1 | N |
new Array(); new Array(size); new Array(element0, element1, ..., elementn); var arr = []; //字面量方式
經測試用字面量方式跟new Array()耗時差很少,使用習慣的差距吧~ new Array()能初始化長度~javascript
prototype 向對象添加屬性和方法java
function em(A,B) { this.A = A; this.B = B; } var e = new em("AA","BB"); em.prototype.C = null; e.C = 20000; console.log(e.C); //20000 console.log(e); //em {A:"AA" , B:"BB" , C : 20000 }
constructor 返回對建立此對象的數組函數的引用數組
function employee(name,job) { this.name = name; this.job = job; } var bill = new employee("Bill"); console.log(typeof bill.constructor); // function console.log(bill.constructor); //function employee(name){this.name=name;this.job=job;}
concat() 鏈接兩個或更多的數組(返回被鏈接數組的副本)函數
var arr = [1, 2, 3]; var arr1 = [6, 7]; console.log(arr.concat(4, 5, arr1)); //[1, 2, 3, 4, 5, 6, 7]
join() 把數組的全部元素放入一個字符串,元素經過指定的分隔符分隔。測試
var arr = ['item 1', 'item 2', 'item 3']; var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; //'<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'
toString() 數組轉換爲字符串(與無參的 join()方法返回的串相同)this
var arr = new Array(3) arr[0] = "A" arr[1] = "B" arr[2] = "C" console.log(arr.toString()); //"A,B,C"
pop() 刪除數組的最後一個元素,數組長度減 1,並返回被刪除的元素的值編碼
若數組爲空,則pop()返回undefined值prototype
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.pop()); //"C" console.log(arr); //["A", "B"]
push() 向數組的末尾添加一個或更多元素,並返回新的長度code
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.push('D')); //4 console.log(arr); ////["A", "B", "C" , "D"]
reverse() 顛倒數組中元素的順序對象
var arr = ["A", "B", "C"]; arr.reverse(); console.log(arr);// ["C", "B", "A"]
slice(start,end) 從數組中返回選定的子數組
var arr = ["A", "B", "C"]; // 從第一個元素開始截取到數組結尾 console.log(arr.slice(1)); //["B", "C"] console.log(arr); //["A", "B", "C"]
splice() 刪除元素,並向數組添加新元素
(直接對數組操做)
① 刪除指定範圍的數組元素:
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr); //["A", "B", "C", "D", "E", "F"] // 刪除第三個元素之後的三個數組元素(包含第三個元素) console.log(arr.splice(2, 3)); //["C", "D", "E"] console.log(arr); //["A", "B", "F"]
② 從指定下標開始插入指定元素(元素個數不限):
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr); //["A", "B", "C", "D", "E", "F"] console.log(arr.splice(2, 0, "AA","BB")); // [] console.log(arr);//["A", "B", "AA" , "BB" , "C", "D", "E", "F"]
③ 刪除指定範圍的數組元素,並用指定元素替換(元素個數不限):
var arr = new Array(6); arr[0] = "A"; arr[1] = "B"; arr[2] = "C"; arr[3] = "D"; arr[4] = "E"; arr[5] = "F"; console.log(arr);//["A", "B", "C", "D", "E", "F"] console.log(arr.splice(2, 1, "AA","BB")); // ["C"] console.log(arr); //["A", "B", "AA", "BB", "D", "E", "F"]
sort() 對數組的元素進行排序
對數組的引用
數組在原數組上進行排序,不生成副本。該方法默認是按照字符編碼(ASCII)的順序進行排序的
var arr = new Array(3); arr[0] = "D"; arr[1] = "S"; arr[2] = "A"; console.log(arr); //["D", "S", "A"] console.log(arr.sort()); //["A", "D", "S"]
自行指定排序規則。以下:
var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] = 40; arr[3] = 25; arr[4] = 1000; arr[5] = 1; console.log(arr); //[10, 5, 40, 25, 1000, 1] //升序 console.log(arr.sort(function (a, b) {return a - b;}));// [1, 5, 10, 25, 40, 1000]
valueOf() 返回相應的原始值
var arr = new Array(2); arr[0] = 10 arr[1] = 5 console.log(arr); //[10, 5] console.log(typeof arr.valueOf()); //object console.log(arr.valueOf()); //[10, 5]
shift() 刪除並返回數組的第一個元素
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.shift()); //A console.log(arr); //["B", "C"]
unshift() 向數組的開頭添加一個或更多元素,並返回新的長度
var arr = ["A", "B", "C"]; console.log(arr); //["A", "B", "C"] console.log(arr.unshift("AA")); //4 console.log(arr);//["AA", "A", "B", "C"]
indexOf() 返回元素在數組中出現位置
額 以前一直覺得只有string有這個方法... 補上補上
var arr = ['orange', '2016', undefined , null ,NaN]; console.log(arr.indexOf('orange')); //0 console.log(arr.indexOf('o')); //-1 console.log(arr.indexOf('2016')); //1 console.log(arr.indexOf(2016)); //-1 console.log(arr.indexOf('undefined')); //-1 console.log(arr.indexOf(null)); //3 console.log(arr.indexOf(NaN)); //-1
以上只是爲了論證這裏的indexOf使用的是嚴格匹配(===),不會像string的indexOf同樣隱式轉換成字符串進行匹配~