I have defined data like an array of objects:
{"Driver":a, "Lap":b, "Position":c}
I want with a loop to insert in the same row all the drivers for each lap (into another data). For example:
{"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}
I know the number of the Laps (in variable maxLaps) and the number of the drivers on variable numDrivers.
I want to make that in a loop because the number of the laps and drivers change in different cases, so I need to do my code generic
asked Jun 10, 2016 at 19:30
LigaVirtual F1
871 gold badge3 silver badges6 bronze badges
1 Answer 1
Define a Javascript object like:
var myObject = { "Driver": a, "Lap": b, "Position": c };
Add new properties to this object as:
myObject.newProperty = d; // now you have: { "Driver": a, "Lap": b, "Position": c, "newProperty": d }
Add a dynamic property as:
myObject[dynamicProperty] = e; // now if your dynamic property is 'test', your object is: { "Driver": a, "Lap": b, "Position": c, "newProperty": d, "test": e }
answered Jun 10, 2016 at 19:34
Rahul Desai
15.5k20 gold badges89 silver badges146 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
I wrestled a bear once.
why lose the double quotes..., technically json is supposed to have them, js just doesnt require them
Rahul Desai
@PootieTang Gotcha. I've updated my answer.
Paul S.
@PootieTang JSON isn't JavaScript, it's just a notation. In JavaScript, JSON would be a string
Matt Burland
@PootieTang There are actually some case where they are required. For example, you could do
var a = { "a-b" : 1 } but without the quotes it would be a syntax error.I wrestled a bear once.
yes, it's a notation... a javascript notation... that's three of the 4 letters in json... let's not get nit-picky here i'm not a noob
|
lang-js
DriverXproperties?