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__;
});
To use the map() function, attach it to an array you want to iterate over. The map() function expects a callback as the argument and executes it once for each element in the array. From the callback parameters, you can access the current element, the current index, and the array itself.
Use d3. map to group items together, creating a hashed array that can be accessed using handy functions like array. get(value) .
map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.
ES6 - Array Method map()map() method creates a new array with the results of calling a provided function on every element in this array.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With