ifancyyou0909
ifancyyou0909

Reputation: 73

What does d3.range.map do?

Given the following code:

var num = 20;
var points = d3.range(num).map(function(d) {
  return {
    x: spread(d),
    y: Math.floor(Math.random() * (height)
  }
});

Can someone please explain to me what d3.range.map is doing here? Specifically, what is d here?

Upvotes: 2

Views: 1187

Answers (1)

Oliver
Oliver

Reputation: 66

It's mapping each value from the created range to an object, so d refers to each value from the range in turn.

More specifically, d3.range(num) will create an array of integers from 0 to 19, so [0, 1, 2, ..., 18, 19]. See here for more information on d3.range.

When .map is called on the array we access each value in turn through the callback function. In the example this is the parameter d. So d will have the value 0, then 1, etc. up to 19. Information on array mapping can be found here.

The callback function is then returning an object for each value in the array. So the variable points will be an array containing objects with an x and y property.

Upvotes: 4

Related Questions