原文地址:綠茶葉的博客
1.push():在數組尾部添加一個或多個元素,返回數組新的長度
arrayObject.push(newelement1,newelement2,....,newelementX)
newelement1:必需要添加的參數。要添加到數組的第一個元素,剩下的參數可選。
var a=['hello','world'];
var length= a.push(2,[4,3]);
console.log(a); //[ 'hello', 'world', 2, [ 4, 3 ] ]
console.log(a.length);//4;
console.log(length);//4;返回值
length=a.push(4);
console.log(a); //[ 'hello', 'world', 2, [ 4, 3 ], 4 ]
console.log(a.length); //5
console.log(length);//5;返回值
2.pop():刪除數組最後一個元素,返回刪除的值,如果數組已經爲空,則 pop() 不改變數組,並返回 undefined 值。
var a=['hello','world',1,2,'hi'];
var arg=a.pop();
console.log(arg);//hi;返回值爲刪除的元素,刪除數組最後一個元素
console.log(a);//[ 'hello', 'world', 1, 2 ]
//當數組爲空
var arr=[];
console.log(arr.pop());//undefined
console.log(arr);//[]
3.shift():刪除數組第一個元素,返回刪除的值,如果數組是空的,那麼 shift() 方法將不進行任何操作,返回 undefined 值
var a=['hello','world',1,2,'hi'];
var arg=a.shift();
console.log(arg);//hello;返回值爲刪除的元素,刪除數組的第一個元素
console.log(a);//[ 'world', 1, 2, 'hi' ]
//當數組爲空
var arr=[];
console.log(arr.shift());//undefined
console.log(arr);//[]
4. unshift():在數組頭部添加一個或多個元素,返回數組新的長度
arrayObject.unshift(newelement1,newelement2,....,newelementX)
newelement1:必需添加的參數,向數組添加的第一個元素,剩餘參數可選
var a=['hello','world'];
var newlength=a.unshift('how',[8,9],'are');
console.log(newlength);//5;返回值爲新數組的長度
console.log(a);//[ 'how', [ 8, 9 ], 'are', 'hello', 'world' ]
5.splice(index,howmany,element1,... ...,elementN):從指定位置刪除指定數量元素並增加新的元素,先執行刪除操作,刪除指定個數的元素,然後再插入元素或數組,splice是直接對原數組進行操作,返回值是被刪除的元素組成的數組.
index:指定位置刪除或插入
howmany:刪除多少元素
elements:插入元素
var a=['hello','world','how','are','you'];
var arg= a.splice(2,1,'?','what');
console.log(arg);//[ 'how' ];返回值刪除的元素
console.log(a);//[ 'hello', 'world', '?', 'what', 'are', 'you' ]
6.concat():把數組原來的元素和新的元素連接起來存放在創建的新數組裏,原數組保持不變,返回創建的新數組
arrayObject.concat(arrayX,arrayX,......,arrayX)
arrayX:必需參數,該參數可以是具體的值,也可以是數組對象。可以是任意多個
var a=['hello','world'];
var arg= a.concat('how','are','you');
console.log(arg);//[ 'hello', 'world', 'how', 'are', 'you' ];返回值爲連接後的新數組
console.log(a);//[ 'hello', 'world' ]
7.slice(start, [end]) ):返回指定數組的一段
start:必需。規定從何處開始選取。如果是負數,那麼它規定從數組尾部開始算起的位置。也就是說,-1 指最後一個元素,-2 指倒數第二個元素,以此類推。
end:可選。規定從何處結束選取。該參數是數組片斷結束處的數組下標。如果沒有指定該參數,那麼切分的數組包含從 start 到數組結束的所有元素。如果這個參數是負數,那麼它規定的是從數組尾部開始算起的元素。
var a=['hello','world','how','are','you'];
var arg= a.slice(3);
console.log(arg);//[ 'are', 'you' ];返回值爲從3開始到數組末尾的數組片段
console.log(a);//[ 'hello', 'world', 'how', 'are', 'you' ]
console.log(a.slice(3,2));//[]
console.log(a.slice(3,4));//[ 'are' ]
console.log(a.slice(3,-1));//[ 'are' ]
console.log(a.slice(3,-2));//[]
console.log(a.slice(0,-3));//[ 'hello', 'world' ]
console.log(a.slice(-5,-3));//[ 'hello', 'world' ]
8. join():將數組的所有元素,用選定的分隔符,轉化爲字符串並連接在一起,返回最後生成的字符串,不指定分隔符默認用逗號
//數組有多個元素
var a=['hello','world',1,2];
console.log(a.join());//hello,world,1,2
console.log(a.join(""));//helloworld12
console.log(a.join(" "));//hello world 1 2
console.log(a.join ("="));//hello=world=1=2
//數組有一個元素
var a=['hello'];
console.log(a.join());//hello
console.log(a.join(""));//hello
console.log(a.join(" "));//hello
console.log(a.join ("="));//hello
9. sort():返回排序後數組。沒有參數,默認按照字母排序
arrayObject.sort(sortby)
var a=['e','a','d','c','b'];
console.log(a.sort());//[ 'a', 'b', 'c', 'd', 'e' ],返回值爲排序後的數組,沒有參數默認爲按照字母排序
console.log(a);//[ 'a', 'b', 'c', 'd', 'e' ]
var b=[1,300,20,250,100];
console.log(b.sort(sort));//[ 1, 20, 100, 250, 300 ]從小到大
var b1=[1,300,20,250,100];
console.log(b1.sort(sortReverse));//[ 300, 250, 100, 20, 1 ],從大到小
function sort(a,b){
return a-b;
}
function sortReverse(a,b){
return b-a;
}
10.reverse() :方法用於顛倒數組中元素的順序。
var a=['e','a','d','c','b'];
console.log(a.reverse());//[ 'b', 'c', 'd', 'a', 'e' ],返回值爲顛倒後的數組
console.log(a);//[ 'b', 'c', 'd', 'a', 'e' ]
11.toSource() :表示對象的源代碼,通常由 JavaScript 在後臺自動調用,並不顯式地出現在代碼中。
12.toString():把數組轉換爲字符串,並返回結果。
var a=['e','a','d','c','b'];
console.log(a.toString());//e,a,d,c,b,返回值爲字符串
console.log(a);// ['e', 'a', 'd', 'c', 'b' ]
13.toLocaleString():把數組轉換爲本地字符串。
arrayObject.toLocaleString()
14.valueOf() :返回 Array 對象的原始值,通常由 JavaScript 在後臺自動調用,並不顯式地出現在代碼中。