Accepted answer

The problem here is the use of an arrow function to access this.

It should be:

.attr("y", function(d){
    return d;

See here the documentation about arrow functions regarding this:

It says:

An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or

To get the current DOM element this in an arrow function, use the second and third arguments combined:

.attr("y", (d, i, n) => {
    return n[i];

Related Query