Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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__; 
});
like image 326
Richard Avatar asked Apr 17 '13 11:04

Richard


People also ask

How do you use the map function?

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.

What does D3 map do?

Use d3. map to group items together, creating a hashed array that can be accessed using handy functions like array. get(value) .

What does map () do JS?

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.

How do I use the map function in ES6?

ES6 - Array Method map()map() method creates a new array with the results of calling a provided function on every element in this array.


1 Answers

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.

like image 90
Lars Kotthoff Avatar answered Sep 20 '22 17:09

Lars Kotthoff