Find Max and Min element from all nested arrays in javascript

I have an array like this:

var arr = [[12,45,75], [54,45,2],[23,54,75,2]];

I want to find out the largest element and the smallest element of all elements of a nested array:

Min should be: 2

and

Max should be 75

I tried the functions below, but they do not work:

    function Max(arrs)
    {
        if (!arrs || !arrs.length) return undefined;
        let max = Math.max.apply(window, arrs[0]), m,
            f = function(v){ return !isNaN(v); };
        for (let i = 1, l = arrs.length; i<l; i++) {
            if ((m = Math.max.apply(window, arrs[i].filter(f)))>max) max=m;
        }
        return max;
    }
    function Min(arrs)
    {
        if (!arrs || !arrs.length) return undefined;
        let min = Math.min.apply(window, arrs[0]), m,
            f = function(v){ return !isNaN(v); };
        for (let i = 1, l = arrs.length; i<l; i++) {
            if ((m = Math.min.apply(window, arrs[i].filter(f)))>min) min=m;
        }
        return min;
    }

It gives Max as 75 and min as 12.

Any guidance would be appreciated.

Also tried other answers in SO, but no one helped.

Answer to Merge / flatten an array of arrays in JavaScript? solves the problem of combining arrays.

While my problem is to keep the array as is and perform operations.

+4
source share
6 answers

Assuming ES6

const arr = [[12,45,75], [54,45,2],[23,54,75,2]];

const max = Math.max(...[].concat(...arr));

const min = Math.min(...[].concat(...arr));

console.log(max);

console.log(min);
Run codeHide result
+12
source

( - )

var flattenedArr = [[12,45,75], [54,45,2],[23,54,75,2] ].toString().split(",").map(Number);

min max

var max = Math.max.apply( null, flattenedArr );
var min = Math.min.apply( null, flattenedArr );

Demo

var flattenedArr = [
  [12, 45, 75],
  [54, 45, 2],
  [23, 54, 75, 2]
].toString().split(",").map(Number);

var max = Math.max.apply(null, flattenedArr);
var min = Math.min.apply(null, flattenedArr);

console.log(max, min);
Hide result
+3

ES5 recursive approach by type checking. It works for deep nested arrays.

var array = [[12, 45, 75], [54, 45, 2], [23, 54, 75, 2]],
    min = array.reduce(function min(a, b) {
        return Math.min(Array.isArray(a) ? a.reduce(min) : a, Array.isArray(b) ? b.reduce(min) : b);
    }),
    max = array.reduce(function max(a, b) {
        return Math.max(Array.isArray(a) ? a.reduce(max) : a, Array.isArray(b) ? b.reduce(max) : b);
    });
    
console.log(min, max);
Run codeHide result

With functions to use as a callback.

function flat(f, v) { return Array.isArray(v) ? v.reduce(f) : v; }
function getMin(a, b) { return Math.min(flat(getMin, a), flat(getMin, b)); }
function getMax(a, b) { return Math.max(flat(getMax, a), flat(getMax, b)); }

var array = [[12, 45, 75], [54, 45, 2], [23, 54, 75, 2]],
    min = array.reduce(getMin),
    max = array.reduce(getMax);
    
console.log(min, max);
Run codeHide result
+2
source

You can simply combine the entire nested array into one array, and then find the minimum and maximum value with Math.min.apply(null, array)andMath.max.apply(null, array)

var arr = [[12,45,75], [54,45,2],[23,54,75,2]];
var merged = [].concat.apply([], arr);
var max = Math.max.apply(null, merged);
var min = Math.min.apply(null, merged);
console.log(max,min)
Run codeHide result
+1
source

A non-concatenation solution that works for any level of nesting

let arr = [[12,45,75], [54,45,2],[23,54,75,2]];

function findMaxFromNestedArray(arr) {
  let max = Number.MIN_SAFE_INTEGER;
  
  for (let item of arr) {
    if(Array.isArray(item)) {
      let maxInChildArray = findMaxFromNestedArray(item);
      if (maxInChildArray > max) {
        max = maxInChildArray;
      }
    } else {
      if (item > max) {
        max = item;
      }
    }
  }
  
  return max;
}

console.log(findMaxFromNestedArray(arr))
Run codeHide result
0
source

The solution with only one abbreviation:

const getMaxMin = (flattened) => {
return flattened.reduce(
        (a, b) => {            
            return {
                maxVal: Math.max(b, a.maxVal),
                minVal: Math.min(b, a.minVal),                
            };
        },
        {
            maxVal: -Infinity,
            minVal: Infinity,            
        }
    );
}
const flatSingle = arr => [].concat(...arr)
const maxMin = getMaxMin(flatSingle(arr))
console.log(maxMin);
0
source

Source: https://habr.com/ru/post/1690372/


All Articles