显示数组中最小数字的索引的 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP