Say we have some test data
var testdata1 = [
[
{x: 1, y: 40},
{x: 2, y: 43},
{x: 3, y: 12},
{x: 4, y: 60},
{x: 5, y: 63},
{x: 6, y: 23}
], [
{x: 1, y: 12},
{x: 2, y: 5},
{x: 3, y: 23},
{x: 4, y: 18},
{x: 5, y: 73},
{x: 6, y: 27}
], [
{x: 1, y: 60},
{x: 2, y: 49},
{x: 3, y: 16},
{x: 4, y: 20},
{x: 5, y: 92},
{x: 6, y: 20}
]
];
and we bind the data to some nested objects, like
var circles = svg.select("circle")
.data(testdata1)
.enter()
.append("circle")
var subsel = circles.selectAll("rect")
.data(function(d) {return d})
.enter()
.append("rect")
The third variable in the d3 anonymous function holds the index of the parent data element (as discussed more in Third variable in D3 anonymous function).
Before adding a transition, the j variable behaves as expected (gives 0,1,2 for each group of 6 elements).
After transitioning, however, the j variable becomes undefined:
subsel.attr("foobar", function(d,i,j) {console.log(d,i,j)})
.transition()
.attr("foobar", function(d,i,j) {console.log(d,i,j)})
What's going on?