It’s the way d3 works ^^
I don’t remember the correct terminology, but basically selectAll returns you all the actual h2. Let’s call them ‘containers’ and say you already have 3 in your graph ( 3 h2).
Then you use the data method, which select the data to put into the containers. Let’s say you have 5.
Now you have 3 containers and 5 data. What’s happen? You need 2 more containers!
That’s what append is made for: you choose the container type you want to use to accomodate the remaining data ( again h2, or whatever you want^^).
In the above example, the selectAll returned 0 ( it’s usual as far as i could see ) so you have 9 ‘emply slot’ to fill - with what? - with the argument of append!
The problem was that passing the string and not the array ( which both have the length property) the ‘empty slots’ were 7 ( "dataset".length ) not 9 (
[12, 31, 22, 17, 25, 18, 29, 14,9].length)