Link to home
Start Free TrialLog in
Avatar of jecommera
jecommeraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

how do I find the largest property in a 2 dimensional array

In Javascript,

var superBlinders = [
  ["Supernova", 12000],
  ["Firelight", 4000],
  ["Solar Death Ray", 6000]
];

the largest holds 1200. how do I find this out of the array.
ASKER CERTIFIED SOLUTION
Avatar of Phil Phillips
Phil Phillips
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, you could just loop through the array.
<script>
var superBlinders = [
  ["Supernova", 12000],
  ["Firelight", 4000],
  ["Solar Death Ray", 6000]
];

current = superBlinders[0];
for(var i = 1; i < superBlinders.length; i++) {
   if (current[1] < superBlinders[i][1]) {
       current = superBlinders[i];
   }
}
console.log('Item with max property is');
console.log(current);
</script>

Open in new window