D3.js: Get an array of the data attached to a selection?

后端 未结 4 494
耶瑟儿~
耶瑟儿~ 2021-02-01 17:39

I\'d like to get an array of all the data attached to a selection in D3.

I\'m working with the example from the General Update pattern, and trying this:

         


        
4条回答
  •  走了就别回头了
    2021-02-01 18:04

    You can simply call .data() without any arguments on a selection to get the bound data. See the documentation:

    If values is not specified, then this method returns the array of data for the first group in the selection. The length of the returned array will match the length of the first group, and the index of each datum in the returned array will match the corresponding index in the selection. If some of the elements in the selection are null, or if they have no associated data, then the corresponding element in the array will be undefined.

    This does not work for the .enter() selection as there are no elements yet to which the data can be bound. In this case, the .map function can be used:

    textEnter.map(function(d) { return d.__data__; });
    

提交回复
热议问题