JavaScript:組合多個數組中最高鍵值到一個單一陣列中
我們需要寫一個接受任意數量數字陣列的 JavaScript 函式。我們的函式應該返回一個從輸入陣列陣列中挑選出來的最大數字的陣列。輸出陣列中的元素數量應該等於原始輸入陣列中包含的子陣列數量。
示例
程式碼如下:
const arr1 = [117, 121, 18, 24]; const arr2 = [132, 19, 432, 23]; const arr3 = [32, 23, 137, 145]; const arr4 = [900, 332, 23, 19]; const mergeGreatest = (...arrs) => { const res = []; arrs.forEach(el => { el.forEach((elm, ind) => { if(!( res[ind] > elm)) { res[ind] = elm; }; }); }); return res; }; console.log(mergeGreatest(arr1, arr2, arr3, arr4));
輸出
控制檯中的輸出如下所示:
[ 900, 332, 432, 145 ]
廣告