JavaScript数组显示最低编号的索引
我们需要编写一个包含数字数组的JavaScript函数。然后,该函数应返回数组中最小数字的索引。
示例
为此的代码将是-
const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3];
const lowestIndex = arr => {
const creds = arr.reduce((acc, val, ind) => {
let { num, index } = acc;
if(val < num){
num = val;
index = ind;
};
return { num, index };
}, {
num: Infinity,
index: -1
});
return creds.index;
};
console.log(lowestIndex(arr));输出结果
控制台中的输出-
6