1.concat
concat()
方法用于合并兩個或多個數組。此方法不會更改現有數組,而是返回一個新數組。
例:
var color = ['red','green','blue'];
var color2 = color.concat('yellow',['black','white']);
console.log(color2)
控制臺輸出為:
["red", "green", "blue", "yellow", "black", "white"]
2.slice
slice()
方法返回一個從開始到結束(不包括結束)選擇的數組的一部分淺拷貝到一個新數組對象,原始數組不會被修改。
例:
var colors = ['red','green','blue','black','white'];
var colors2 = colors.slice(1) //green,blue,black,white
var colors3 = colors.slice(1,4) //green,blue,black
3.splice
splice()
方法始終會返回一個數組,該數組包含從原始數組中刪除的項(如果沒有刪除任何項,則返回一個空數組),用途最廣,有如下3種:
. 刪除:需指定2個參數,要刪除的第一項位置和要刪除的項數
. 插入:需提供3個參數,起始位置、0(要刪除的項數)和要插入的項,如要插入多個項 ,再傳入第四,五...
. 替換:需指定3個參數,起始位置、要刪除的項數和要插入的任意數量的項
例:
var colors = ['red','green','blue'];
var removed = colors.splice(0,1);
console.log(colors); //green,blue
console.log(removed); //red
var removed = colors.splice(1,0,'black');
console.log(colors); //green,black,blue
console.log(removed); // 返回空數組
var removed = colors.splice(0,2,'yellow','white');
console.log(colors); //yellow,white,blue
console.log(removed); //red,green
4.join
join()
使用指定的字符串用來分隔數組字符串
例:
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join("."))
輸出結果為:
George.John.Thomas
5.push
push()
可接受任意類型的參數,將它們逐個添加到數組的末尾,并返回數組的長度
例:
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.push("James") + "<br />")
document.write(arr)
輸出結果為:
George,John,Thomas
4
George,John,Thomas,James
6.pop
pop()
從數組的末尾移除最后一項,減少數組的length值,返回移除的項
例:
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr)
document.write("<br />")
document.write(arr.pop())
document.write("<br />")
document.write(arr)
輸出結果為:
George,John,Thomas
Thomas
George,John
7.shift
shift()
移除數組中的第一個項并且返回該項,同時將數組的長度減一。
例:
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)
輸出結果為:
George,John,Thomas
George
John,Thomas
8.unshift
unshift()
在數組的前端添加任意個項,并返回新數組的長度。
例:
var arr = new Array()
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.unshift("William") + "<br />")
document.write(arr)
輸出結果為:
George,John,Thomas
4
William,George,John,Thomas
9. reverse
reverse()
方法用于顛倒數組中元素的順序
例:
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.reverse())
輸出結果為:
George,John,Thomas
Thomas,John,George
10.string
String()
函數把對象的值轉換為字符串。
例:
var test1 = new Boolean(1);
var test2 = new Boolean(0);
var test3 = new Boolean(true);
var test4 = new Boolean(false);
var test5 = new Date();
var test6 = new String("999 888");
var test7 = 12345;
document.write(String(test1) + "<br />");
document.write(String(test2) + "<br />");
document.write(String(test3) + "<br />");
document.write(String(test4) + "<br />");
document.write(String(test5) + "<br />");
document.write(String(test6) + "<br />");
document.write(String(test7) + "<br />");
輸出結果為:
true
false
true
false
Mon Sep 17 2018 15:35:58 GMT+0800 (中國標準時間)
999 888
12345