Accepted answer

Since you're using D3 v3 you can use the third argument, which is the parent's index, to set your classes:

.attr("class", function(d,i,p){
    return "class_" + p;

Check the snippet:

var data = [[1,1,1,1],[1,1,1,1],[1,1,1,1]];
var svg ="body");
var g = svg.append("g")
var parentElement = g.selectAll(".parent")
    .attr("class", "parent");

    .data(function(d,i) { return d; })
    .attr("class", function(d,i,p){
  console.log("circle_" + p);
  return "circle_" + p;
<script src=""></script>

PS: this will not work on D3 v4 (see here).

Related Query