Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

arrays - Iterating over objects in Javascript

I have an obejct of the form:

var test = {"2011":{"10":4,"9":9,"8":15,"7":11,"6":11,"5":13,"4":9,"3":5,"2":9,"1":4,"0":20},"2010":{"11":9,"10":23,"9":58}}

I want to iterate over this so I can hit each year and month combo. I have:

var years = d3.keys(test),
    months = d3.range(12),
    data = [];

So, as a Javascript newbie, to sanity check things I inspect years.

document.writeln(years)
2011,2010 

Cool, but when I try to iterate over the years array to pull data out of test the elements I do what I think is correct to test, but the elements are lost:

for(var y in years) {
  document.writeln(y);
}
0,1

How do I iterate over years to get the actual elements (2011,2010) in them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

AFAIK, var y in years will give you the keys to the array years. years[y] should give you the actual value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...