0
嘿,我一直在試圖從數組中返回2個最小的數字,而不管索引。你能幫我解決嗎?如何從js中的數組中獲取最小的兩個數字?
嘿,我一直在試圖從數組中返回2個最小的數字,而不管索引。你能幫我解決嗎?如何從js中的數組中獲取最小的兩個數字?
Array#slice
得到前兩個元素(最小的那個)。var arr = [5, 4, 7, 2, 10, 1],
res = arr.sort((a,b) => a - b).slice(0, 2);
console.log(res);
嘿雅,謝謝:) – Ivayloi
您是不是要找'array'? –
你好,對,對不起 – Ivayloi