Here is an array I'm working with:
var arr = [[12,45,75], [54,45,2],[23,54,75,2]];
I am trying to determine the largest and smallest elements in this nested array:
The minimum value should be: 2
while the maximum should be: 75
I attempted using the following functions but they did not yield the correct results:
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;
}
Unfortunately, these functions incorrectly identify the maximum as 75 and the minimum as 12.
If you have any suggestions or guidance, please feel free to share.
I have also explored other solutions on Stack Overflow but none seem to address my specific issue.
The response provided at Merge/flatten an array of arrays in JavaScript? tackles merging arrays instead of performing operations while maintaining the original array structure.