Richard Richard - 5 months ago 42
Javascript Question

d3.js: how to use map function?

How can I use

d3.map()
to get
[10, 12]
out of the following array?

var mydata = [
{ '__data__' : 10 },
{'__data__' : 12 }
];


I have been trying this, but it doesn't work:

var mymap = d3.map(mydata, function(d) {
return d.__data__;
});

Answer

You can't -- d3.map() isn't for mapping a function across an array, but a shim for hashes. Briefly, while objects can be used like hashes, there are situations when unexpected behaviour can occur. A new Javascript standard proposes a solution to this, and until it is implemented, d3.map() can be used to the same effect.

More information in the documentation.