删除对象属性

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JSON 对象</h1>
  6.  
  7. <p>如何删除 JSON 对象的属性。</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": {
  17. "car1":"Porsche",
  18. "car2":"BMW",
  19. "car3":"Volvo"
  20. }
  21. }
  22. delete myObj.cars.car3;
  23.  
  24. for (i in myObj.cars) {
  25. x += myObj.cars[i] + "<br>";
  26. }
  27.  
  28. document.getElementById("demo").innerHTML = x;
  29. </script>
  30.  
  31. </body>
  32. </html>