如何将元素添加到javascript对象?
JavaScript对象表示法就像
varcar={名称:“奔驰”,“品牌”:“奔驰”,型号:“敞篷”};
如果我们需要加上奔驰车的价格,则代码如下。
示例
<html> <body> <p id="demo"></p> <script> var car = {name:"Benz", "Brand": "Mercedes", model:"open roof"}; car.price = "50,000$", car.color = "Black" document.write(JSON.stringify(car)); </script> </body> </html>
输出结果
{"name":"Benz","Brand":"Mercedes","model":"open roof","price":"50,000$","color":"Black"}