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?

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

yea... that's an ASP tag
Just use brackets instead of []
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]});
}

4 Comments

thanks i changed my answer, I was thinking in json and now I have learned the diff! totally valid without the quotes.. as elaborated here: stackoverflow.com/a/381151/471339
Sweet... How do i iterate through the data array now?
Use the same way but now your data array contains objects. data[i].label or data[i].data
for (i=0; i<data.length; i++) { alert(data[i].label); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.