Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

javascript - function is not iterable with "new Map()"

I'm creating a network graph with d3.js.

I want to group the nodes just like this example. The example uses d3 v4, but I'm using v6.

In the migration guide it says the (d3).map() doesn't work anymore(it gives me an error when I use .map()) and I have to use new Map(). But when I do this I get the following error:

Uncaught TypeError: function is not iterable (cannot read property Symbol(Symbol.iterator)) at new Map ()

This is my code atm:

const groupIds = new Set(nodes.map(function(n) {
  //get ids
}))
  .values()
  new Map(function(departmentId) {
    return {
      departmentId: departmentId,
      count: nodes.filter(function(n) {
        //some code
      }).length
    };
  })
  .filter(function(department) {return department.count > 0;})
  new Map(function(department) {return department.departmentId;});

I also tried let map = new Map(), but gives the same error.

How can I use the new Map() in this context?

When I use .map() instead of new Map() it says:

Uncaught TypeError: (intermediate value).values(...).map is not a function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Map is not related to map: Map is a constructor used to create an instance of Map, and .map is a method used process every element of array.

If you use Set just to create an array of unique values you can omit .values execution and just convert your result Set to array:

[...new Set([1,1,1,1,1,2,1])]
// or 
Array.from(new Set([1,1,1,1,1,2,1]))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...