使用 for 循环遍历数组
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JSON 数组</h1>
- <p>使用 for 循环遍历数组</p>
- <p id="demo"></p>
- <script>
- var myObj, i, x = "";
- myObj = {
- "name":"Bill Gates",
- "age":62,
- "cars":[ "Porsche", "BMW", "Volvo" ]
- };
- for (i = 0; i < myObj.cars.length; i++) {
- x += myObj.cars[i] + "<br>";
- }
- document.getElementById("demo").innerHTML = x;
- </script>
- </body>
- </html>