1. push
push()將新元素追加到一個(gè)數(shù)組中,并將數(shù)組的長度+1。
var color1 = ['red', 'yellow', 'blue'];
color1.push('green');
console.log(color1);//["red", "yellow", "blue", "green"]
console.log(color1.length);//4
2. pop
push()將數(shù)組中最后一個(gè)元素移除终抽,,并將數(shù)組的長度-1戳表。
var color1 = ['red', 'yellow', 'blue','green'];
color1.pop();
console.log(color1);//["red", "yellow", "blue"]
console.log(color1.length);//3
3. shift
shift()將數(shù)組中第一個(gè)元素移除(前端)桶至,,并將數(shù)組的長度-1。
var color1 = ['red', 'yellow', 'blue','green'];
color1.shift();
console.log(color1);//[ "yellow", "blue",'green']
console.log(color1.length);//3
4. unshift
unshift()將新元素追加到一個(gè)數(shù)組的前端,并將數(shù)組的長度+1匾旭。
var color1 = ['red', 'yellow', 'blue','green'];
color1.unshift('gray');
console.log(color1);//["gray", "red", "yellow", "blue", "green"]
console.log(color1.length);//5
5. join
join()作用是把數(shù)組元素(對(duì)象調(diào)用其toString()方法)使用參數(shù)作為連接符連接成一字符串镣屹,不會(huì)修改原數(shù)組內(nèi)容.
var color1 = ['red', 'yellow', 'blue','green'];
console.log(color1.join('+'));//"red+yellow+blue+green"
console.log(color1.join('和'));//"red和yellow和blue和green"
console.log(color1.join(' '));//"red yellow blue green"
console.log(color1);//["red", "yellow", "blue", "green"]
6. splice
splice() 方法通過刪除現(xiàn)有元素和/或添加新元素來更改一個(gè)數(shù)組的內(nèi)容。
語法arrayObject.splice(index,howmany,item1,.....,itemX)
-
index
指定修改的開始位置(從0計(jì)數(shù))季率。如果超出了數(shù)組的長度野瘦,則從數(shù)組末尾開始添加內(nèi)容;如果是負(fù)值飒泻,則表示從數(shù)組末位開始的第幾位(從-1計(jì)數(shù))鞭光;若只使用index參數(shù)而不使用howmany、item泞遗,如:array.splice(start) 惰许,表示刪除[index,end]的元素史辙。 -
howmany
整數(shù)汹买,表示要移除的數(shù)組元素的個(gè)數(shù)。如果是 0聊倔,則不移除元素晦毙。這種情況下,至少應(yīng)添加一個(gè)新元素耙蔑。如果 howmany大于index 之后的元素的總數(shù)见妒,則從 index后面的元素都將被刪除(含第 index 位)。
如果howmany被省略甸陌,則其相當(dāng)于(arr.length - index)须揣。 -
itemx
要添加進(jìn)數(shù)組的元素,從index 位置開始。如果不指定钱豁,則 splice() 將只刪除數(shù)組元素耻卡。
splice方法使用deleteCount參數(shù)來控制是刪除還是添加:
start參數(shù)是必須的,表示開始的位置(從0計(jì)數(shù))牲尺,如:index=0從第一個(gè)開始卵酪;index>= array.length-1表示從最后一個(gè)開始。
1谤碳、從index位置開始刪除[index溃卡,end]的元素。
array.splice(start)
2估蹄、從index位置開始刪除[start塑煎,Count]的元素。
array.splice(start, howmany)
3臭蚁、從index位置開始添加item1, item2, ...元素最铁。
array.splice(index, 0, item1, item2, ...)
var a = [1,2,3,4,5];
a.splice(1,0,9,99,999);
console.log(a.length); //8
console.log(a);//[1, 9, 99, 999, 2, 3, 4, 5]
a.splice(1,3,8,88,888);
console.log(a.length);//8
console.log(a);//[1, 8, 88, 888, 2, 3, 4, 5]
6. sort
sort()對(duì)數(shù)組的元素進(jìn)行排序讯赏,并返回?cái)?shù)組。 sort 排序不一定是穩(wěn)定的冷尉。默認(rèn)排序順序是根據(jù)字符串Unicode碼點(diǎn)漱挎。
語法
arr.sort()
arr.sort(compareFunction)
compareFunction
可選。用來指定按某種順序進(jìn)行排列的函數(shù)雀哨。如果省略磕谅,元素按照轉(zhuǎn)換為的字符串的各個(gè)字符的Unicode位點(diǎn)進(jìn)行排序。
var a=[5,4,3,2,1]
a.sort()
console.log(a) //[1, 2, 3, 4, 5]
7. reverse
reverse()
方法用于將數(shù)組逆序雾棺,與之前不同的是它會(huì)修改原數(shù)組.
var nub = ['one', 'two', 'three'];
nub.reverse();
console.log(nub); //['three', 'two', 'one']
8. concat
concat() 方法將一個(gè)或多個(gè)字符串與原字符串連接合并膊夹,形成一個(gè)新的字符串并返回。
concat 方法并不影響原字符串捌浩。
語法
str.concat(string2, string3[, ..., stringN])
var hello = "Hello, ";
console.log(hello.concat("任務(wù)20")); //*"Hello, 任務(wù)20"