join("-"):回傳所有元素串連,用「-」串聯(也可以設定成別的)
reverse():回傳元素反轉陣列
sort():回傳排序過的陣列(依照16進未排序)
length:回傳元素個數,注意沒有括號
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>07arrayObject.html</title>
<script>
//Array Declaration 1
var balls = new Array();//括弧裡不用放陣列大小,因為沒有影響
//alert(balls.length);
balls[0] = "basketball";
balls[1] = "baseball";
balls[2] = "tennis";
balls[3] = "volleyball";
balls[4] = 1;
//alert(balls.length);
document.write("balls:" + balls + "<hr>");
//Array Declaration 2
var balls1 = new Array("basketball", "baseball", "tennis", "volleyball", 2)
document.write("balls1:" + balls1 + "<hr>");
//Array Declaration 3
var balls2 = ["basketball", "baseball", "tennis", "volleyball", 3]
document.write("balls2:" + balls2 + "<hr>");
//另一種列印方式:
for(var i=0;i<balls2.length;i++)
document.write("balls2[" + i + "]=" + balls2[i] + "<br>");
document.write("<hr>")
//用in的方式列印
for (var obj in balls2)
document.write("balls2[" + obj + "]=" + balls2[obj] + "<br>");
document.write("<hr>")
//跟陣列相關的語法
var balls3=["basketball","baseball","tennis","volleyball",123];
document.write("join('-')=" + balls3.join("-") + "<br>");
document.write("reverse().join('=>')="+balls3.reverse().join("=>")+"<br>");
document.write("sort()=" + balls3.sort() + "<br>");
document.write("length=" + balls3.length + "<br>");
</script>
</head>
<body>
</body>
</html>
balls:basketball,baseball,tennis,volleyball,1
balls1:basketball,baseball,tennis,volleyball,2
balls2:basketball,baseball,tennis,volleyball,3
balls2[0]=basketball
balls2[1]=baseball
balls2[2]=tennis
balls2[3]=volleyball
balls2[4]=3
balls2[0]=basketball
balls2[1]=baseball
balls2[2]=tennis
balls2[3]=volleyball
balls2[4]=3
join('-')=basketball-baseball-tennis-volleyball-123
reverse().join('=>')=123=>volleyball=>tennis=>baseball=>basketball
sort()=123,baseball,basketball,tennis,volleyball
length=5
留言列表