最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年10月24日 17:04:54   投稿:goldensun  
這篇文章主要介紹了向JavaScript的數(shù)組中添加元素的方法小結(jié),分別舉了一些JS數(shù)組操作的例子,基本需要的朋友可以參考下

在數(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> 

測試結(jié)果:

Lemon,Pineapple,Banana,Orange,Apple,Mango


在數(shù)組的第2位置添加一個元素 - 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>   

測試結(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>  

測試結(jié)果:

Banana,Orange,Apple,Mango,Kiwi

下面就提供一下上文中的一些函數(shù)

數(shù)組的創(chuàng)建

var arrayObj = new Array(); //創(chuàng)建一個數(shù)組
var arrayObj = new Array([size]); //創(chuàng)建一個數(shù)組并指定長度,注意不是上限,是長度
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); 創(chuàng)建一個數(shù)組并賦值

要說明的是,雖然第二種方法創(chuàng)建數(shù)組指定了長度,但實際上所有情況下數(shù)組都是變長的,也就是說即使指定了長度為5,仍然可以將元素存儲在規(guī)定長度以外的,注意:這時長度會隨之改變。

數(shù)組元素的添加

arrayObj. push([item1 [item2 [. . . [itemN ]]]]);// 將一個或多個新元素添加到數(shù)組結(jié)尾,并返回數(shù)組新長度
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// 將一個或多個新元素添加到數(shù)組開始,數(shù)組中的元素自動后移,返回數(shù)組新長度
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//將一個或多個新元素插入到數(shù)組的指定位置,插入位置的元素自動后移,返回""。

數(shù)組的元素的訪問

var testGetArrValue=arrayObj[1]; //獲取數(shù)組的元素值
arrayObj[1]= "這是新值"; //給數(shù)組元素賦予新的值
數(shù)組元素的刪除
arrayObj.pop(); //移除最后一個元素并返回該元素值
arrayObj.shift(); //移除最前一個元素并返回該元素值,數(shù)組中元素自動前移
arrayObj.splice(deletePos,deleteCount); //刪除從指定位置deletePos開始的指定數(shù)量deleteCount的元素,數(shù)組形式返回所移除的元素

數(shù)組的截取和合并

arrayObj.slice(start, [end]); //以數(shù)組的形式返回數(shù)組的一部分,注意不包括 end 對應(yīng)的元素,如果省略 end 將復(fù)制 start 之后的所有元素
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //將多個數(shù)組(也可以是字符串,或者是數(shù)組和字符串的混合)連接為一個數(shù)組,返回連接好的新的數(shù)組

數(shù)組的拷貝

arrayObj.slice(0); //返回數(shù)組的拷貝數(shù)組,注意是一個新的數(shù)組,不是指向
arrayObj.concat(); //返回數(shù)組的拷貝數(shù)組,注意是一個新的數(shù)組,不是指向

數(shù)組元素的排序

arrayObj.reverse(); //反轉(zhuǎn)元素(最前的排到最后、最后的排到最前),返回數(shù)組地址
arrayObj.sort(); //對數(shù)組元素排序,返回數(shù)組地址

相關(guān)文章

最新評論

黔西| 荃湾区| 麻城市| 商城县| 三原县| 广灵县| 芦山县| 依兰县| 依兰县| 普宁市| 恩施市| 太仓市| 彭山县| 伊吾县| 禹城市| 阿拉善盟| 游戏| 秀山| 百色市| 临夏县| 百色市| 龙胜| 中江县| 来凤县| 攀枝花市| 白沙| 湾仔区| 平凉市| 饶阳县| 涞水县| 永年县| 曲麻莱县| 大连市| 积石山| 左权县| 桂平市| 历史| 当阳市| 栾川县| 清苑县| 鹤山市|