selection.node()returns only the first node. Can we get an array of all nodes from the selection?
EDIT Added code to help us.
- Trying with
each()is the only one that produces a query output, although quite verbose. - The call
sel[0]also returns an array with DOM nodes, but it is hacked (depending on the internal structure of the library) and includes an undesired parentNode field.
var data= [1,2,3,4]
var sel = d3.select("li")
.data(data)
.enter().append("li").html(identity);
function identity(d){return d}
console.log(sel);
var res1 = sel.node();
console.log(res1);
var res2 = [];
function appendToRes2(){
res2.push(this);
}
sel.each(appendToRes2);
console.log(res2);
var res3 = sel[0];
console.log(res3);
var res4 = d3.selectAll(sel);
console.log(res4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Run codeHide resultEDIT 2 Why do I want to do this?
To cause an array techniques , for example reduce, and mapto the DOM nodes. D3 provides filter, but to use others, I first need to extract the node array from the selection.