2016-02-08 63 views
1

我嵌套我的数据是这样如何访问嵌套的数据值D3

var nested_data = d3.nest() 
    .key(function(d) { return d.Country; }) 
    .entries(CSV); 

,并已发现,当我绑定数据我可以使用数组的值,如果我用这个功能

name.selectAll('.name') 
    .data(function(d) { 
     return d.values; <--- Accesses the nested data 
    }) 
    .text(function(d) { 
     return d.Name;  <--- Nested value 
    }) 

但是我在我的项目的其他地方遇到了麻烦。另一个我需要的嵌套值是一个名为['starting point']的字符串。我怎样才能访问这个值在这个d3.filter()例如?

var filter = nested_data.filter(function(d) { 
    return ("Island" == d['starting point']) 
}); 

我想尝试:d.values [ '起点']

或者甚至创建一个新的变量

变种nested_values = nested_data(函数(d){返回d。值});

但没有一个是有效的。我应该在这里做什么?

回答

0

可以在这样的嵌套数据筛选数据:

nested_data.map(function(m) { 
    //for each group do the filter on each value 
    m.values = m.values.filter(function(d) { 
    //return true for those having starting point as island. 
    return d['starting point'] == 'Island'; 
    }) 
}) 

工作代码here

+0

我没想到的是,再次感谢。我发现很难找到这方面的例子。我可以做什么,当我再次遇到这个: nested_data.forEach(函数(d){ if(uniqueLocations.indexOf(d ['starting point'])<0) uniqueLocations.push(d ['起点']) }); – user3821345

+0

做这样的事'var unique = []; nested_data.map(函数(M){ m.values.forEach(函数(d){ \t如果(unique.indexOf(d [ '起点'])<0) \t unique.push(d [”起点']); //添加自从不存在于数组 }) }) ' 这里提琴https://jsfiddle.net/cyril123/jp90dssj/2/ – Cyril

+1

我要读更多关于d3.map()。再次感谢 – user3821345