Languages
[Edit]
EN

JavaScript - find max value in array of arrays

6 points
Created by:
Aran-Busby
532

In this article, we would like to show you how to find the maximum value in an array of arrays using JavaScript.

1. Custom method example

// ONLINE-RUNNER:browser;

function findMax(/* many arguments here ... */) {
  function checkEntry(entry) {
    if (entry instanceof Array) {
        return findMax.apply(null, entry);
      }
      return entry;
  }
  if (arguments.length > 0) {
    var result = checkEntry(arguments[0]);
    for (var i = 1; i < arguments.length; ++i) {
      var value = checkEntry(arguments[i]);
      if (value > result) {
        result = value;
      }
    }
    return result;
  }
  return NaN;
}

// Usage example:

console.log(findMax(1, 2, 3, [4, 5, 6],[7, 8, [9, 10]]));

See also

  1. JavaScript - Math.max() method example

Alternative titles

  1. JavaScript - how to find max value in array of arrays?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join