获取最大最小数值以及下标

/**
 * 获取数组最大值下标
 */
const findMaxIndex = (numberList) => {
  let maxValue = parseFloat(numberList[0]);
  let minValue = parseFloat(numberList[0]);
  let maxIndex = 0;
  let minIndex = 0;
  for (let i = 0; i < numberList.length; i++) {
    let current = parseFloat(numberList[i]);
    if (current > maxValue) {
      maxValue = current;
      maxIndex = i;
    } else if (current < minValue) {
      minValue = current;
      minIndex = i;
    }
  }
  return maxIndex
}

 

posted @ 2022-07-14 11:24  Nyan  阅读(44)  评论(0编辑  收藏  举报