在 JavaScript 中从多维数组中选取最大元素


给定一个多维数组,任务是在 JavaScript 中从中选取最大元素。

多维数组是在数组内部的数组。只要数组内部有数组,它就将作为多维数组。

以下是一维数组:

const arr = ["Welcome", "to", "tutorials", "point"];
const arr = [1, 5, 12, 67, 99];

这就是多维数组的样子:

const array=[["Mike tyson", "Vijay"], ["ananya", "charmee"], ["Lion", "Tiger"]];

这就是我们如何从多维数组中访问元素:

const array = [["Mike tyson", "Vijay"], ["ananya", "charmee"], ["Lion", "Tiger"]];

//This will access the first subarray
document.write(array[0]); // This will print ["Mike tyson", "Vijay"]

// This will access the first item in second subarray
document.write(array[1][0]); // This will print "ananya"

// This will access the second element in the third subarray
document.write(array[2][1]); // This will print "Tiger"

使用 Math.max() 方法

我们可以使用Math.max()方法和for循环从多维数组中选取最大元素。

Math.max()方法返回输入参数中最高的值。如果传递的参数不是数字,则返回NaN

document.write(Math.max(65, 45, 21));
// output: 65

document.write(Math.max("hello", "varma"));
// output: NaN

示例

在下面的示例中,我们创建了一个多维数组和一个空数组,该数组将保存每个子数组中的所有最大数字。Math.max()方法将选取每个子数组中的最大数字。扩展运算符 (…) 将从数组 `array[x]` 获取所有元素,并将其作为参数传递给 Math.max() 方法。然后,最高数字将被推入空数组。

<!DOCTYPE html>
<html>
<head>
   <title>Picking the largest elements from multidimensional array in JavaScript</title>
   <button onClick = "func()">Click!</button>
   <p id = "para"> </p>
</head>
<body>
   <script>
      const array = ([[23, 45, 22], [45, 55, 11], [66, 99, 200], [43, 76, 2022]]);
      function func(){
         function fun(array) {
            var EmpArr = []
            var x = 0;
            var len = array.length;
            for (x; x < len; x++) {
               EmpArr.push(Math.max(...array[x]))
            }
            return EmpArr;
         }
         document.getElementById("para").innerHTML = fun(array);
      };
   </script>
</body>
</html>

正如我们在输出中看到的,每个子数组中的最高数字都被推入空数组。

使用 forEach() 方法

forEach()方法为数组中的每个元素调用指定的函数。此方法不会对空元素执行。

语法

以下是forEach()方法的语法:

array.forEach(function(currentValue, index, arr));

以下是此函数的参数:

  • function,此参数是一个将在每个元素上执行的函数。

  • currentValue,数组中正在处理的当前元素。

  • index,此参数是当前元素的索引。

  • arr,当前元素的数组。

示例

在下面的示例中,我们创建了一个多维数组和一个空数组来保存数组中最大的数字。我们将回调函数(arr)传递到 forEach() 方法中,并在数组上调用forEach()

扩展运算符 (…) 将获取数组中的所有元素,并将数组的每个元素作为参数传递给Math.max(),后者将选取最大的数字。这些最大数字将被推入空数组。

<!DOCTYPE html>
<html>
<head>
   <title>Picking the largest elements from multidimensional array in JavaScript</title>
   <button onClick = "func()">Click!</button>
   <p id = "para"> </p>
</head>
<body>
   <script>
      const array = ([[87, 54, 76], [76, 98, 90], [12, 45, 82], [65, 98, 78]]);
      function func(){
         function fun(array) {
            var EmpArr = []
            array.forEach(function(arr){
               EmpArr.push(Math.max(...arr))
            })
            return EmpArr;
         }
         document.getElementById("para").innerHTML = fun(array);
      };
   </script>
</body>
</html>

在输出中,我们看到我们已经使用forEach()迭代了数组,并使用Math.max()从数组中提取了最大数字。

使用 apply() 方法

JavaScript 中的apply()方法将使用给定的值和作为数组或类数组对象提供的参数调用描述的函数。

要获取数组中的最大数字,我们使用 Math.max() 方法,并且我们可以使用任意数量的参数。

document.write(Math.max(2, 6, 12, 6));
// Output: 12 

但是,当您将数字数组传递给 Math.max() 函数时,它将返回 NaN。

const array = [32, 65, 12, 64];
document.write(Math.max(array));
// Output: NaN

为避免这种情况,我们使用apply()方法。

const array = [32, 65, 12, 64];
document.write(Math.max.apply(null, array));
//Output: 65

使用 map() 方法

map()方法通过为每个数组元素调用指定的函数来创建一个新数组。此方法仅对数组中的每个元素调用一次,并且也不会修改原始数组。

语法

以下是 map() 方法的语法。

array.map(function(currentValue, index, arr))

以下是此函数的参数:

  • function,此参数是一个将在每个元素上执行的函数。

  • currentValue,数组中正在处理的当前元素。

  • index,此参数是当前元素的索引。

  • arr,特定元素的数组。

示例

以下是程序,我们在这里创建了一个多维数组,并使用map()方法遍历每个子数组,并在其中使用Math.max()方法从数组中选取最大数字。

<!DOCTYPE html>
<html>
<head>
   <title>Picking the largest elements from multidimensional array in JavaScript</title>
   <button onClick = "func()"> Click me! </button>
   <p id = "para"> </p>
</head>
<body>
   <script>
      const array = ([[87, 54, 76], [76, 98, 90], [12, 45, 82], [65, 98, 78]]);
      function func(){
         function LargeElement(array) {
            return array.map(function(subArr) {
               return Math.max.apply(null, subArr);
            });
         }
         document.getElementById("para").innerHTML = LargeElement(array);
      };
   </script>
</body>
</html>

我们在输出中看到,map()方法迭代了数组中的每个元素,并在Math.max()的帮助下选取了最大数字。

更新于:2022年12月19日

2K+ 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.