Why are the .selectAll() D3-created circles in my SVG said to be 1 element using .length?

时间秒杀一切 提交于 2019-12-25 09:58:51

问题


I suspect that the answer lies somewhere between my ignorance of what kind of object I'm actually working and the inner darkness of D3 and/or SVG.

I can see 30 circles on the screen. So I go in the console like this.

var circles  = d3.select("#svg1").selectAll("circle");  
>undefined  

circles;  
>[Array[30]]...  

circles.length;  
>1

I know I'm targeting the right elements because executing .remove() on the set clears them from the screen. But what's up with the count?


回答1:


var circles = d3.select("#svg1").selectAll("circle");

circles is an array of array, hence the length is 1. That is the length of the outer array. If you want to get the count of circles selected then you should use d3s built-in method size.

var count = d3.select("#svg1").selectAll("circle").size();

This will give you the expected result.



来源:https://stackoverflow.com/questions/35101259/why-are-the-selectall-d3-created-circles-in-my-svg-said-to-be-1-element-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!