1

(This is following up on this question.) Super-brief/basic Q: is there any way that I can concatenate d.(something) with a numeric value? I'm using a CSV and my variable names follow a pattern (for year 2012 = y2012, year 2002 = y2002, etc).

I'd like to refer to the current year being examined after each keydown. For example:

svg.selectAll("circle")
    .transition(1500)
    .attr("r", function(d) {
    return Math.sqrt(parseInt("d.y"+year) * 0.0004);  
})

Console's telling me "d.y"+year is not kosher. I've tried defining it as a variable, testing it out in the console (where it returns "d.y2012"), but it still breaks the function (d).

goblin-esque
  • 465
  • 5
  • 15
  • possible duplicate of [Dynamically access object property using variable](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Paulo Scardine Sep 15 '14 at 18:08

1 Answers1

3

In javascript, d.something is the same as d["something"].

In your case, you can try d["y"+year] instead of "d.y"+year.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153