国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

向JavaScript的數(shù)組中添加元素的方法小結(jié)

2019-11-20 11:24:26
字體:
供稿:網(wǎng)友

在數(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ù)組并賦值

要說明的是,雖然第二種方法創(chuàng)建數(shù)組指定了長(zhǎng)度,但實(shí)際上所有情況下數(shù)組都是變長(zhǎng)的,也就是說即使指定了長(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ù)組的元素的訪問

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ù)組地址

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宕昌县| 德令哈市| 麻阳| 广灵县| 南康市| 桓仁| 灵山县| 广灵县| 石河子市| 宝山区| 宝应县| 喜德县| 彩票| 土默特左旗| 安福县| 宁远县| 西城区| 晋江市| 普兰县| 依兰县| 平罗县| 兴化市| 林西县| 伽师县| 白河县| 南木林县| 玛曲县| 宁国市| 伊宁县| 泸溪县| 渝中区| 乌审旗| 荣成市| 泸定县| 奉新县| 特克斯县| 慈利县| 二连浩特市| 清原| 郓城县| 绥芬河市|