Javascript基礎教程之數(shù)組 array
字符串,數(shù)值,布爾值都屬于離散值(scalar),如果某個變量是離散的,那么任何時候它只有一個值。
如果想使用變量存儲一組值,就需要使用數(shù)組(array)。
數(shù)組是由多個名稱相同的樹值構成的集合,集合中每個數(shù)組都是數(shù)組的元素(element),可以使用變量team存儲團隊里每個成員的名字。
在JavaScript中,數(shù)組使用關鍵字 Array聲明創(chuàng)建,同事還可以聲明變量的長度length。例如
var aTeam = new Array(12);//聲明變量的長度
在無法預知數(shù)組的最終個數(shù)時,聲明數(shù)組可以不指定具體個數(shù)。例如:
var aTeam = new Array();//數(shù)組最終個數(shù)未知的情況下,可以不聲明具體的個數(shù)
aTeam[0] = 1414;
aTeam[1] = "北京";
aTeam[2] = 0x4;
aTeam[3] = "i can";
aTeam[4] = "red";
aTeam[5] = "blue";
aTeam[6] = "orange";
另外,可以直接創(chuàng)建數(shù)組
var aTeam = new Array("111","blue","red","beijing");
同字符串一樣,數(shù)組也可以使用length獲取和指定數(shù)組的長度。
var aTeam = new Array("111","blue","red","beijing" );
document.write(aTeam[1]+"<br>");
document.write(aTeam.length +"<br>")
令注:能更深刻的理解數(shù)組。
var aTeam = new Array("111","blue","red","beijing" );
aTeam[20] = "12415"
document.write(aTeam[20]+"<br>");
document.write(aTeam.length +"<br>")
另外, 數(shù)組還可以用[ ]來定義。中間使用逗號隔開。
sTeam = [10,"5565","北京",33263,"red"]
document.write(sTeam[3]) //輸出 33263
數(shù)組可以使用toString()方便的轉化
sTeam = [10,"5565","pking",33263,"red"]
document.write(sTeam.toString()) //
//輸出結果 10,5565,pking,33263,red
document.write(typeof(ss));
//輸出結果 string
如果數(shù)組轉化為字符串不想使用逗號連接 ,可使用join()方法。
sTeam = [10,"5565","pking",33263,"red"]
ss = sTeam.join("-");
dd =sTeam.join("][")
//輸出結果 10,5565,pking,33263,red
document.write(ss);
document.write(dd);
//輸出 10-5565-pking-33263-red 10][5565][pking][33263][red
對于字符串,JavaScript使用split()轉化為數(shù)組
var fruit = "apple,2151,orange";
sfruit = fruit.split(",")
document.write(sfruit); //輸出 apple,2151,orange
document.write(sfruit.join("-")); //輸出apple-2151-orange
接上例,javascript提供了reverse()方法使數(shù)組反轉。
var fruit = "apple,2151,orange";
sfruit = fruit.split(",")
document.write(sfruit); //輸出 apple,2151,orange
document.write(sfruit.join("-")+"<br>"); //輸出apple-2151-orange
document.write(sfruit.reverse()+"<br>");輸出 orange,2151,apple
document.write(sfruit.reverse().toString()+"<br>");輸出apple,2151,orange
對于字符串反轉,javascript沒有直接的轉化方法,我們可以利用split()將字符串轉為數(shù)組,利用rerverse()進行反轉,再利用join進行連接,達到字符串反轉。
var fruit = "2151,orange,apple";
var sfruit = "iambeijing123";
document.write(fruit.split(",").reverse()+"<br>");//2151,orange,apple
document.write(sfruit.split("").reverse().join("")+"<br>");//輸出iambeijing123
利用sort()可以使數(shù)組元素進行(字母順序)排序。
fruit = ["orange2","2151","orange","apple"]
document.write(fruit.sort());//輸出結果2151,apple,orange,orange2
關于push()和pop()的用法
sfruit = new Array();
sfruit.push("red");
sfruit.push("green");
sfruit.push("oragen");
sfruit.push("blue");
document.write(sfruit.length + sfruit.toString()+"<br>");
var wfruit = sfruit.pop();
document.write(wfruit+"<br>")
document.write(sfruit.toString())
如上,javascript將數(shù)組看成了一個堆棧,通過push()和pop()進行壓棧和出棧處理。
相關文章
在JS中操作時間之getUTCMilliseconds()方法的使用
這篇文章主要介紹了在JavaScript中操作時間之getUTCMilliseconds()方法的使用,是JavaScript入門學習中的基礎知識,需要的朋友可以參考下2015-06-06
解讀JavaScript中 For, While與遞歸的用法
本篇文章對JavaScript中 For, While與遞歸的用法進行了詳細的分析介紹。需要的朋友參考下2013-05-05
JavaScript數(shù)據(jù)結構和算法之二叉樹詳解
這篇文章主要介紹了JavaScript數(shù)據(jù)結構和算法之二叉樹詳解,本文講解了二叉樹的概念、二叉樹的特點、二叉樹節(jié)點的定義、查找最大和最小值等內容,需要的朋友可以參考下2015-02-02
JavaScript中Number.MIN_VALUE屬性的使用示例
這篇文章主要介紹了JavaScript中Number.MIN_VALUE屬性的使用示例,是JS入門學習中的基礎知識,需要的朋友可以參考下2015-06-06

