向JavaScript的數(shù)組中添加元素的方法小結(jié)
在數(shù)組的開(kāi)頭添加新元素 - 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ù)組開(kāi)始,數(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開(kāi)始的指定數(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ù)組地址
相關(guān)文章
微信小程序訪問(wèn)node.js接口服務(wù)器搭建教程
這篇文章主要給大家分享了微信小程序訪問(wèn)node.js接口服務(wù)器的搭建教程,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)跟著小編一起看看吧。2017-04-04JavaScript 學(xué)習(xí)筆記二 字符串拼接
JavaScript 字符串拼接的一些知識(shí)點(diǎn)分析,對(duì)于提高效率等,都是非常值得一看的。2010-03-03在JavaScript中操作時(shí)間之setYear()方法的使用
這篇文章主要介紹了在JavaScript中操作時(shí)間之setYear()方法的使用,是JS入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06JavaScript高級(jí)程序設(shè)計(jì) 學(xué)習(xí)筆記 js高級(jí)技巧
JavaScript高級(jí)程序設(shè)計(jì) 學(xué)習(xí)筆記 js高級(jí)技巧,學(xué)習(xí)js的朋友可以參考下。2011-09-09javascript類(lèi)型系統(tǒng)——undefined和null全面了解
下面小編就為大家?guī)?lái)一篇javascript類(lèi)型系統(tǒng)——undefined和null全面了解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07