I have a nested array that looks like this: Nested Array
I would like to traverse through the nested array and find the max value of the 'value' field situated in grouped_ratings > values > value
I have tried the following and can't seem to figure out the issue.
var yMax = d3.max(grouped_ratings, function(g) {
return d3.map(g.values, function(v) {
return d3.max(v.value)
})
});
CodePudding user response:
Change d3.map
to d3.max
and in the inner-most function, you can just return v.value
. Here's what it looks like:
d3.max(grouped_ratings, function (g) {
return d3.max(g.values, function (v) {
return v.value;
});
});
Or as a one-liner: d3.max(grouped_ratings, g => d3.max(g.values, v => v.value))
These snippets work when grouped_ratings
has the format
grouped_ratings = [
{
key: '2011',
values: [
{ key: '1', value: 151 },
{ key: '2', value: 12 },
{ key: '3', value: 314 },
{ key: '4', value: 178 },
{ key: '5', value: 31 },
{ key: '6', value: 1 },
{ key: '7', value: 1 },
]
},
{
key: '2012',
values: [
{ key: '1', value: 203 },
{ key: '2', value: 4 },
{ key: '3', value: 41 },
{ key: '4', value: 87 },
{ key: '5', value: 13 },
{ key: '6', value: 10 },
{ key: '7', value: 100 },
]
},
]