6

I have an JavaScript array of arrays:

[[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]]

How can I get the biggest element based on value of second element in sub array?

In case above I want to get element:

[-782, 5]

Because 5 is biggest second value in all sub arrays. If there will more than one sub arrays with the bigger second value, I'd like to get first one.

Mariusz

1

4 Answers 4

3

Perform a descending sort based on the second element of each element of the array.

var array = [[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]];


array.sort(function(a, b) {
    return b[1] - a[1];
});

console.log(array[0]);

jsFiddle Demo

Please note that while this solution is terse, using sort just for finding the largest or smallest element would be much slower than a for loop that finds the largest/smallest element in a single iteration.

1
  • In an interview your will not be allowed to use sort , it is also highly not recommended to use sort as it is very slow.
    – Andy
    Nov 13, 2017 at 1:02
1
var array = [[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]];
var maxElementIndex = 0;
for (var i = 0; i < array.length; i++) {
    if (array[i][1] > array[maxElementIndex][1]) {
        maxElementIndex = i;
    }
}

console.log(array[maxElementIndex]);
1

Since this is tagged you could also use d3.greatest() from the d3-array module v2.3.0 (emphasis mine):

Returns the greatest element of the specified iterable according to the specified comparator or accessor.

By providing an accessor function that returns the second element from each element's array d3.greatest() will return the element having the greatest value comparing all second elements.

const data = [[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]];

const greatest= d3.greatest(data, d => d[1]);

console.log(greatest);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3-array.min.js"></script>

Note, however, that at the time of writing d3.greatest() is not yet part of the D3 v5 main bundle, you have you use the d3-array module. It should be included in the upcoming v6, though.

0
var target = [[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]]
var max = 0, tmp = 0;
var max_entry, tmp_entry;
while (target.length > 0){
  tmp_entry = target.pop();
  tmp = tmp_entry[1];
  if (tmp > max){
      max = tmp;
      max_entry = tmp_entry;
  }
}
alert(max_entry);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.