0

I'm trying to get data into an array. The current temp array looks like this:

tmp[0][0] = "NY"
tmp[0][1] = "52"
tmp[1][0] = "FL"
tmp[1][1] = "25"

What i'm trying to do is push the data into a new array named data. The final structure for data should look like this:

data = [
  { label: "NY",  data: 52},
  { label: "FL",  data: 25},
]];

I can't seem to get that going. Here's what i tried:

for(var i = 0; i < tmp.length; i++) {
    data<%=r1%>.push([label: tmp[i][0],  data: tmp[i][1]]);
}

all i'm hitting is brick walls and my brain hurts. Any ideas?

Damien
  • 4,093
  • 9
  • 39
  • 52

2 Answers2

2

<%=r1%> is an ASP tag? Do something like this:

for(var i = 0; i < tmp.length; i++) {
    data<%=r1%>.push({label: tmp[i][0],  data: tmp[i][1]});
}

Now your data contains objects, so you can access them in this way:

for(var i = 0; i < data.length; i++) {
    console.log(data[i].label);
    console.log(data[i].data);
}
Ragnar
  • 4,393
  • 1
  • 27
  • 40
1

your square brackets would push an array not an object, so replace the square brackets with curly brackets, like:

for(var i = 0; i < tmp.length; i++) {
    data<%=r1%>.push({label: tmp[i][0], data: tmp[i][1]});
}
brobas
  • 596
  • 4
  • 5