使用 for 循环遍历数组

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JSON 数组</h1>
  6.  
  7. <p>使用 for 循环遍历数组</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var myObj, i, x = "";
  13. myObj = {
  14. "name":"Bill Gates",
  15. "age":62,
  16. "cars":[ "Porsche", "BMW", "Volvo" ]
  17. };
  18.  
  19. for (i = 0; i < myObj.cars.length; i++) {
  20. x += myObj.cars[i] + "<br>";
  21. }
  22. document.getElementById("demo").innerHTML = x;
  23. </script>
  24.  
  25. </body>
  26. </html>