删除数组 JavaScript 中的最小数字
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/16 16:44:17
我们需要编写一个 JavaScript 函数,该函数接受一个数字数组。该数字应找到数组中的最小元素并将其就地删除。
其代码为 −
const arr = [2, 1, 3, 2, 4, 5, 1];
const removeSmallest = arr => {
const smallestCreds = arr.reduce((acc, val, index) => {
let { num, ind } = acc;
if(val >= num){
return acc;
};
ind = index;
num = val;
return { ind, num };
}, {
num: Infinity,
ind: -1
});
const { ind } = smallestCreds;
if(ind === -1){
return;
};
arr.splice(ind, 1);
};
removeSmallest(arr);
console.log(arr);
以下是控制台上的输出 −
[ 2, 3, 2, 4, 5, 1 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

