我已将数据定义为对象数组:
{"Driver":a, "Lap":b, "Position":c}
我希望用一个循环在同一行中插入每一圈的所有驱动程序(转换为另一个数据)。例如:
{"Lap":1,"Driver1":2,"Driver2":1,"Driver3":3}
{"Lap":2,"Driver1":1,"Driver2":2,"Driver3":3}
{"Lap":3,"Driver1":3,"Driver2":2,"Driver3":1}
{"Lap":4,"Driver1":2,"Driver2":1,"Driver3":3}
我知道Laps的数量(在变量maxLaps中)和变量numDrivers上的驱动器数量。
我想在循环中制作它,因为不同情况下圈数和驱动程序的数量会发生变化,所以我需要将代码设为通用
答案 0 :(得分:0)
定义一个Javascript对象,如:
var myObject = { "Driver": a, "Lap": b, "Position": c };
将新属性添加到此对象:
myObject.newProperty = d; // now you have: { "Driver": a, "Lap": b, "Position": c, "newProperty": d }
添加动态属性:
myObject[dynamicProperty] = e; // now if your dynamic property is 'test', your object is: { "Driver": a, "Lap": b, "Position": c, "newProperty": d, "test": e }