D3:根据条件跳过项目

Pau*_*aul 3 d3.js

我有一个如下所示的数据数组:

mydata = [ {
           "title": "key1",
           "description": "some description 1",
           "visible": "1",
         },
         {
           "title": "key2",
           "description": "some description 2",
           "visible": "0",
         },
         {
           "title": "key3",
           "description": "some description 3",
           "visible": "1",
         }
  ]
Run Code Online (Sandbox Code Playgroud)

...并使用以下代码:

        var chart = svg.selectAll("g.chart")
        .data(mydata, function(i, d)
        {
            return d;
        })
        .enter()
        .append("svg:g")
        .attr("class", "chart")
        .attr("style", "position:fixed");
Run Code Online (Sandbox Code Playgroud)

使用以下代码,如何跳过“可见”= 0 的项目?

基本上,以可见性 = 1 显示所有内容?

谢谢

Lar*_*off 5

您可以使用.filter()

.data(mydata.filter(function(d) { return d.visible == "1"; }))
Run Code Online (Sandbox Code Playgroud)