I am using d3 v6 rollups to create a nested data structure. I have an array ‘feature_vars’ that contains the variables I want to use for nested grouping. Right now, I can do this for two variables like below.
JavaScript
x
4
1
feature_vars = ['v0','v1']
2
data = d3.rollups(data, v => v.length,
3
d => d[feature_vars[0]], d=>d[feature_vars[1]]])
4
The problem is feature_vars is dynamic and can contain any number of variables. So, how do I make it generic so that is works for any number of variables in the feature_vars array?
Advertisement
Answer
One way would be to use the destructuring assignment:
JavaScript
1
4
1
const feature_vars = ['v0','v1'],
2
maps = feature_vars.map(fv => function(d) {return d[fv]}),
3
data = d3.rollups(data, v => v.length, maps)
4