0

Hi Everyone,

i build an Array for a JSON Object.

for(var v in data) {
    records.push({
        month: v,
        ship : data[v]

    });
}

Before, i looped through a Database which gaves me, in every loop a String with a Ship Name.

var ship = "RANDOMSHIPNAME";

Now, i want to insert the string var ship into to the JSON Array.

But, this is not working, the JSON Object wirting still "ship" in it, instead of the real string value.

The JSON String should look like this, if everything would work fine:

 "records":[
         {"month":"12","RANDOMSHIPNAME":2},
         {"month":"5","RANDOMSHIPNAME":5},
         {"month":"8","RANDOMSHIPNAME_TWO":1}
    ]

Regards and Thanks,

Sven

  • 1
    Since you can't use a variable as a property name, you can update the code inside the loop to something like: `var obj = {month: v}; obj[ship] = data[v]; records.push(obj);` This way `ship` can be a variable. – jwatts1980 Apr 23 '15 at 14:49
  • Great! That's it, thank you very much ! – tearitdown Apr 23 '15 at 15:12

0 Answers0