在數(shù)組的開頭添加新元素 - unshift()
源代碼:
<!DOCTYPE html><html><body><p id="demo">Click the button to add elements to the array.</p><button onclick="myFunction()">Try it</button><script>function myFunction(){var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.unshift("Lemon","Pineapple");var x=document.getElementById("demo");x.innerHTML=fruits;}</script><p><b>Note:</b> The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will be <em>undefined</em>.</p></body></html>
測(cè)試結(jié)果:
Lemon,Pineapple,Banana,Orange,Apple,Mango
在數(shù)組的第2位置添加一個(gè)元素 - splice()
源代碼:
<!DOCTYPE html><html><body><p id="demo">Click the button to add elements to the array.</p><button onclick="myFunction()">Try it</button><script>function myFunction(){var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice(2,0,"Lemon","Kiwi");var x=document.getElementById("demo");x.innerHTML=fruits;}</script></body></html>
測(cè)試結(jié)果:
Banana,Orange,Lemon,Kiwi,Apple,Mango
數(shù)組的末尾添加新的元素 - push()
源代碼:
<!DOCTYPE html><html><body><p id="demo">Click the button to add a new element to the array.</p><button onclick="myFunction()">Try it</button><script>var fruits = ["Banana", "Orange", "Apple", "Mango"];function myFunction(){fruits.push("Kiwi")var x=document.getElementById("demo");x.innerHTML=fruits;}</script></body></html>
測(cè)試結(jié)果:
Banana,Orange,Apple,Mango,Kiwi
下面就提供一下上文中的一些函數(shù)
數(shù)組的創(chuàng)建
var arrayObj = new Array(); //創(chuàng)建一個(gè)數(shù)組
var arrayObj = new Array([size]); //創(chuàng)建一個(gè)數(shù)組并指定長(zhǎng)度,注意不是上限,是長(zhǎng)度
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); 創(chuàng)建一個(gè)數(shù)組并賦值
要說(shuō)明的是,雖然第二種方法創(chuàng)建數(shù)組指定了長(zhǎng)度,但實(shí)際上所有情況下數(shù)組都是變長(zhǎng)的,也就是說(shuō)即使指定了長(zhǎng)度為5,仍然可以將元素存儲(chǔ)在規(guī)定長(zhǎng)度以外的,注意:這時(shí)長(zhǎng)度會(huì)隨之改變。
數(shù)組元素的添加
arrayObj. push([item1 [item2 [. . . [itemN ]]]]);// 將一個(gè)或多個(gè)新元素添加到數(shù)組結(jié)尾,并返回?cái)?shù)組新長(zhǎng)度
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// 將一個(gè)或多個(gè)新元素添加到數(shù)組開始,數(shù)組中的元素自動(dòng)后移,返回?cái)?shù)組新長(zhǎng)度
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//將一個(gè)或多個(gè)新元素插入到數(shù)組的指定位置,插入位置的元素自動(dòng)后移,返回""。
數(shù)組的元素的訪問(wèn)
var testGetArrValue=arrayObj[1]; //獲取數(shù)組的元素值
arrayObj[1]= "這是新值"; //給數(shù)組元素賦予新的值
數(shù)組元素的刪除
arrayObj.pop(); //移除最后一個(gè)元素并返回該元素值
arrayObj.shift(); //移除最前一個(gè)元素并返回該元素值,數(shù)組中元素自動(dòng)前移
arrayObj.splice(deletePos,deleteCount); //刪除從指定位置deletePos開始的指定數(shù)量deleteCount的元素,數(shù)組形式返回所移除的元素
數(shù)組的截取和合并
arrayObj.slice(start, [end]); //以數(shù)組的形式返回?cái)?shù)組的一部分,注意不包括 end 對(duì)應(yīng)的元素,如果省略 end 將復(fù)制 start 之后的所有元素
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //將多個(gè)數(shù)組(也可以是字符串,或者是數(shù)組和字符串的混合)連接為一個(gè)數(shù)組,返回連接好的新的數(shù)組
數(shù)組的拷貝
arrayObj.slice(0); //返回?cái)?shù)組的拷貝數(shù)組,注意是一個(gè)新的數(shù)組,不是指向
arrayObj.concat(); //返回?cái)?shù)組的拷貝數(shù)組,注意是一個(gè)新的數(shù)組,不是指向
數(shù)組元素的排序
arrayObj.reverse(); //反轉(zhuǎn)元素(最前的排到最后、最后的排到最前),返回?cái)?shù)組地址
arrayObj.sort(); //對(duì)數(shù)組元素排序,返回?cái)?shù)組地址
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注