Search code examples
d3.js

How to find max value from a 2D matrix using D3.js


I have data in the following format:

var data=[[5,2,3,6],[10,22,10,5],[2,3,4,5],[50,30,20,13]];

That means it's a M*N array. How can I use d3.max from this array? I need to get a single value, i.e., 50. I was trying with

var max=d3.max(data,function(d){return d});

But it's not working. Can anyone help me?

Thanks in advance


Solution

  • Your code will try to find the maximum of four arrays. What you actually need is another call to d3.max within the other call:

    var max = d3.max(data, function (d) {
        return d3.max(d);
    });