语法:
对象[属性] = "值";
// 在属性的地方可以放变量
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>动态增加key</title> </head> <body> <script type="text/javascript"> var data = { text: "呀呀呀呀呀呀呀呀", what: "v恩发了可能八万人", }; for (let i = 1; i <= 10; i++) { data["lable" + i] = "值" + i; }; console.log(data); </script> </body> </html>
|