H. Trujillo
H. Trujillo

Reputation: 437

D3 select specific node by data

Scenario

I have a D3 tree drawn with 4 nodes. I want to select a node by its data -> uuid

Current Solution

console.log(
  Object.keys(d3.selectAll('.node-circle').filter(d => d.data.id == '55ac64b257c14bbf9d7525cf0b095f60'))
)
console.log(Object.keys(d3.select('.node-circle').datum()))

The second console.log returns ['data', 'height', 'depth', 'parent', 'x', 'y', 'id', 'x0', 'y0'] , while the first returns ['_groups', '_parents']

Question

How can I get the same data from the filtered nodes as from the single-selected node?

Upvotes: 1

Views: 5702

Answers (2)

pmkro
pmkro

Reputation: 2550

Going off what @mgraham suggested. According to the docs, you could also do this. Will get the data from only the selection that meets the boolean.

var selectionData = d3.selectAll(".node-circle")
  .select(function(d, i) { return d.data.id == '55ac64b257c14bbf9d7525cf0b095f60' ? this : null; }).datum();

Upvotes: 2

mgraham
mgraham

Reputation: 6207

Surely it's just combining the two approaches you have, filter the nodes to the id you want and then return its datum?

var yourAnswer = d3.selectAll(".node-circle")
    .filter(function(d) { 
        return d.data.id == '55ac64b257c14bbf9d7525cf0b095f60';
    })
    .datum()
;

Upvotes: 3

Related Questions