score:2

Accepted answer

The problem is, that you use the variable i as loopvariable of your for-loop and as parameter of the anonymous function in the filter part.

Inside the anonymous function, only the parameter i can be accessed and your loop variable is invisble because it has the same name. This is called variable shadowing.

As the documentation of D3 explaines here, the second parameter that is given to the callback of the filter function is the index of the node. Therefore inside your callback you access thresholds[0] for the first node of nodes, thresholds[1] for the second node and so on.

Since you don't need the index of the current node for your filter, the easiest fix would be to leave out the i parameter like so:

for(i = 0 ;  i < 5 ; i++) { //going through the creation of 5 subsets 
  groups[i] = nodes.filter(function(d) { 
    return d.Shape_Roundness > thresholds[i] && d.Shape_Roundness <= thresholds[i + 1];
    //Get the nodes "d" which have a roundness superior to a minimum threshold and inferior to a maximum threshold
  });
}

This way the i variable inside the callback will refer to your loop variable.


Related Query

More Query from same tag