0

Is there a way to use d3.max or d3.min to find the maximum or minimum of a specific column in a multidimensional javascript array?

var arr = new Array();
for(var i=0;i<10;i++){
    arr.push([i,Math.random()]);
}

From this code I'd like to do something like d3.max(arr[*][1]) (with a wildcard, or otherwise) to get the maximum value of the second column, but specifying an index seems to require designating a row first, and then a column, which seems to limit the d3.max routine to row operations only.

I know I can code a separate function to extract this information, but am wondering if there is something built into the D3 API or in Javascript that will do this.

Topher
  • 498
  • 5
  • 17
  • May I ask why you use new Array() instead of var arr = []; – Data Aug 22 '15 at 17:50
  • They're effectively the same, i use both, but overall just like the way it looks. – Topher Aug 22 '15 at 18:42
  • They are the same, but arr = [] is the more efficient. "VM needs to use extra CPU cycles to figure out what new Array actually does" - http://stackoverflow.com/a/7375418/1837472 – Data Aug 22 '15 at 18:50
  • Thanks, I'll try to remember that when I make programs that tax the CPU, but for now things are far simpler. – Topher Aug 22 '15 at 19:48
  • everything will 'tax the CPU', your task is reduce the overhead from the get-go. – Data Aug 22 '15 at 19:59

1 Answers1

0

I found one way to do this using the map method:

arr.map(function(x){return x[1];})

This will return an array of the items at index "1" in each mapped element. With this, applying the d3.max function works fine. I guess its the solution for now.

Topher
  • 498
  • 5
  • 17