user3821345
user3821345

Reputation: 648

How to access nested data Values in D3

I have nested my data like this

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

and have found that I can access the values of my array if I use this function when I bind the data

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

However I am having trouble using this elsewhere in my project. Another nested value that I need is a string called ['starting point']. How can I can access this value to use in this d3.filter() for example?

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

I'd like to try: d.values['starting point']

or even creating a new variable

var nested_values = nested_data(function(d) { return d.values});

but none of this is valid. What should I do here?

Upvotes: 3

Views: 2657

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

You can filter your data on the nested data like this:

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';
  })
})

Working code here

Upvotes: 1

Related Questions