JS數組的常見用法實例
更新時間:2015年02月10日 09:47:16 作者:woshisap
這篇文章主要介紹了JS數組的常見用法,實例分析了javascript針對數組的遍歷、排序與字符串分割成數組等用法,非常具有實用價值,需要的朋友可以參考下
本文實例講述了JS數組的常見用法。分享給大家供大家參考。具體方法如下:
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>數組方法的應用</title>
<script type="text/javascript">
var arrayFruit = new Array("apple", "orange", "peach", "bannaner");
document.write("排序前的數組 <br/>");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
arrayFruit.sort(); //對數組進行排序
document.write("排序后的數組:<br />");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
document.write("排序后連接的字符串:<br />");
var str = arrayFruit.join("-");
document.write(str);
var strFruit = "apple,bannaner,orange,peach";
var tempFruit = strFruit.split(",");
document.write("字符串分割成的數組<br />");
for(var i in tempFruit) {
document.write(tempFruit[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>數組方法的應用</title>
<script type="text/javascript">
var arrayFruit = new Array("apple", "orange", "peach", "bannaner");
document.write("排序前的數組 <br/>");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
arrayFruit.sort(); //對數組進行排序
document.write("排序后的數組:<br />");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
document.write("排序后連接的字符串:<br />");
var str = arrayFruit.join("-");
document.write(str);
var strFruit = "apple,bannaner,orange,peach";
var tempFruit = strFruit.split(",");
document.write("字符串分割成的數組<br />");
for(var i in tempFruit) {
document.write(tempFruit[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
主要介紹數組的遍歷和字符串的分割,感興趣的朋友可以測試運行一下。
希望本文所述對大家的javascript程序設計有所幫助。
相關文章
JavaScript使用循環(huán)和分割來替換和刪除元素實例
一個JavaScript實例,使用循環(huán)和分割來替換和刪除元素,很簡單,但很實用,建議初學者學習下2014-10-10
利用Javascript判斷操作系統(tǒng)的類型實現不同操作系統(tǒng)下的兼容性
在通過Javascript實現客戶端和服務端的交互時,有時候需要對操作系統(tǒng)進行判斷,以便實現不同操作系統(tǒng)下的兼容性;從而實現網站在跨平臺瀏覽時候保持良好的用戶體驗,感興趣的朋友可以了解下啊,或許對你有所幫助2013-01-01

