在javascript中查找所有嵌套数组中的Max和Min元素

时间:2022-09-28 10:58:45

I have a array like so:

我有一个像这样的数组:

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

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

我想找出嵌套数组中所有元素中最大的元素和最小的元素:

The min should be: 2

分钟应该是:2

and

Max should be 75

马克斯应该是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 out Max as 75 and min as 12.

它给出Max为75,min为12。

Any guidance will be appreciated.

任何指导将不胜感激。

Also tried other answers in SO but none help.

在SO中尝试了其他答案,但没有任何帮助。

The answer at Merge/flatten an array of arrays in JavaScript? resolves the problem of merging arrays.

在JavaScript中合并/展平数组数组的答案?解决了合并数组的问题。

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

而我的问题是保持数组不变并执行操作。

5 个解决方案

#1


11  

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);

#2


3  

You can flatten the array first (advantage - will work for nested arrays at multiple levels)

您可以先将数组展平(优点 - 适用于多个级别的嵌套数组)

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

Then get the min and max from the flattened array

然后从展平的阵列中获取最小值和最大值

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);

#3


2  

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

通过检查类型的ES5递归方法。它适用于深层嵌套数组。

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);

With functions for using as 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);

#4


1  

You can simply merged all the nested array into a single array and then find minimum and maximum value by using Math.min.apply(null, array) and Math.max.apply(null, array)

您可以简单地将所有嵌套数组合并到一个数组中,然后使用Math.min.apply(null,array)和Math.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)

#5


0  

Solution without concatenation, which 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))

#1


11  

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);

#2


3  

You can flatten the array first (advantage - will work for nested arrays at multiple levels)

您可以先将数组展平(优点 - 适用于多个级别的嵌套数组)

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

Then get the min and max from the flattened array

然后从展平的阵列中获取最小值和最大值

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);

#3


2  

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

通过检查类型的ES5递归方法。它适用于深层嵌套数组。

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);

With functions for using as 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);

#4


1  

You can simply merged all the nested array into a single array and then find minimum and maximum value by using Math.min.apply(null, array) and Math.max.apply(null, array)

您可以简单地将所有嵌套数组合并到一个数组中,然后使用Math.min.apply(null,array)和Math.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)

#5


0  

Solution without concatenation, which 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))