显示最小数字索引的 JavaScript 数组

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/24 13:32:17

我们需要编写一个 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

相关文章


有用资源