曲径通幽论坛

标题: 使用 for ... in 循环遍历数组 [打印本页]

作者: beyes    时间: 2011-12-14 13:12
标题: 使用 for ... in 循环遍历数组
可以用 for ... in 循环遍历数组中的每个元素,测试代码如下:
[code=javascript]<script type="text/javascript">
var WebSite = new Array();

WebSite["first"] = "www";
WebSite["second"] = "sina";
WebSite["third"] = "com";
WebSite["fourth"] = "cn";

var indexKey;

for (indexKey in WebSite) {
    document.write( "indexKey is " + indexKey + "<br>" );
    document.write( "item value is " + WebSite[indexKey] + "<br><br>" );
    }
</script>[/mw_shl_code]
运行输出:
indexKey is first
item value is www

indexKey is second
item value is sina

indexKey is third
item value is com

indexKey is fourth
item value is cn





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2