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.