score:4

Accepted answer

You can filter a selection with .filter(). In your case, this would look something like this.

var selected = circle.filter(function(d) {
         return d.name == "Amazon" || d.name == "Foxconn";
});

Now you can operate on that selection, where this will be the circle elements and the data is accessible in the usual way.

selected.each(function(d) {
  // d contains the data for the node and this is the circle element
});

score:1

when you use enter() to append node, i think you should add some attribute to label the

node such as a id

<!doctype html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>Template Index</title>
</head>
<body>

  <ul id="comp">
  </ul>

    <script src="http://d3js.org/d3.v3.min.js"></script>
  <script>
  var company = [
    { id: "IBM", value: "222"},
    { id: "MS", value: "222"},
    { id: "Google", value: "222"}
  ];
  var comp = d3.select("#comp").selectAll(".com")
    .data(company)
    .enter()
    .append("li")
    .attr("id", function (d) { return "id" + d.id; })
    .text(function (d) { return d.id; });

  var ibm = d3.select("#idIBM").datum();
  console.log(ibm); // Object { id="IBM", value="222"}
  </script>
</body>
</html>

then you can get the selection throw d3

then get the object data binded in selection by datum()

update: i update the file and tested in firefox chrome and ie10 it works well


Related Query