boopbeepboop
boopbeepboop

Reputation: 1

d3.nest().key().map().each is not a function

I'm using D3 v4. I've searched far and wide for this but found nothing. I reproduced the problem at Mister Nester but someone pointed out that Mr. N is using D3 v3. Still, the data set and code example they use will still create 'my' error if used with D3 v4.

Here's the code to paste in:

d3.nest()
  .key(function(d) { return d.year; })
  .map(data).each(function(values, key) { return 0;});

The browser console catches the error about

"not a function": Uncaught TypeError: d3.nest(...).key(...).map(...).each is not a function


If you search through the unminified v4 code you will find an example of map.each used exactly as I've used it:

map.each(function(v, k) { array.push({key: k, values: entries(v, depth)});

Then the code that defines map.each says the same thing:

each: function(f) {
    for (var property in this) if (property[0] === prefix) f(this[property], property.slice(1), this);
  }

This is my first stackoverflow question. I tried to be thorough. Please let me know if I should do things differently.

Upvotes: 0

Views: 2225

Answers (1)

Mark
Mark

Reputation: 108512

The Mister Nester tool you link to is running d3 version 3. From the version 3 docs:

nest.map(array[, mapType])

Applies the nest operator to the specified array, returning an associative array.

That's a plain old javascript object, which does not have a .each method.

The closest to what you want in version 3 is:

 d3.nest()
   .key(function(d) { return d.year; })
   .map(data, d3.map)
   .forEach(function(values, key) { return 0;});

Upvotes: 1

Related Questions