如果我有以下代码:
<html>
<body>
<script type="text/javascript">
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
var x = 1;
document.write("Value of x: " + x + "<br /><br />");
for (x in mycars)
{
document.write(x + ": " + mycars[x] + "<br />");
}
document.write("<br />Value of x: " + x + "<br />");
document.write("Number of cars: " + mycars.length);
</script>
</body>
</html>
我得到输出:
Value of x: 1
0: Saab
1: Volvo
2: BMW
Value of x: 2
Number of cars: 3
在不将 for-in 循环更改为 for 循环的情况下,有没有办法让它不显示第一个元素(“Saab”)?我希望输出为:
Value of x: 1
1: Volvo
2: BMW
Value of x: 2
Number of cars: 3