Stephen
Stephen

Reputation: 1057

How do I get the index number from the array in d3?

In the following code how do I get the number for the index/row that is 'd'?

    var docs = svg.selectAll("g")
        .data(dataset)
      .enter()
        .append("g")
        .attr("id", function(d) { return d["Identifier"]})
        .attr("num", function(d) { return d}) // this
        .attr("x", function(d) { return xScale(d3.time.format.iso.parse(d["UserDate"]))})
        .attr("y", function(d) { return xScale(d3.time.format.iso.parse(d["UserDate"]))});

Upvotes: 3

Views: 6327

Answers (1)

Xeijp
Xeijp

Reputation: 873

simply use :

function(d,i){ ... }

e.g.

.attr('id', function(d,i){return 'item'+i;})

Upvotes: 6

Related Questions