在 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.