2014-03-04 34 views
2

我有類似下面的數據陣列:D3:跳過項目根據病情

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", 
     } 
    ] 

...並用下面的代碼:

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

用下面的代碼,我怎麼能跳過「可見」= 0的項目?

基本上,顯示一切與可見性= 1?

感謝

回答

3

您可以使用.filter()

.data(mydata.filter(function(d) { return d.visible == "1"; })) 
+0

我喜歡簡單的解決方案。謝謝一堆! – Paul