簡體   English   中英

從特定位置之間的數組中獲取最大值

[英]Get max value from array between certain positions

我試圖從數組中返回最大值,但我只需要數組中某些位置的最大值:例如。

var array = [1,2,3,4,5] 並且我需要將 array[0] 到 array[2] 的最大值的和相加到 array[3] 到 array[4](所以totalScore = max(array[0] ...array[2]) + max(array[3] ...array[4])

有沒有辦法用 Math.max.apply 做到這一點? 我下面的代碼 + 筆在這里: https : //codepen.io/stdobrescu/pen/mdJdBbG?editors=1111

  $("input[type='radio']").click(function() {
    var question = document.getElementsByClassName("toggle");
    var totalScore = 0;
    var questionValue = [0];  

    for (i = 0; i < question.length; i++) {
      if (question[i].type == "radio") {
        if (question[i].checked) { 
          questionValue[i] = parseInt(question[i].value, 10);
          }
        highestVal(questionValue);
        totalScore = calcScore(questionValue);
        questionValue = questionValue.filter(n=>n!==undefined);
        $("#score").html(totalScore);
      }
    }
  });

 function highestVal (valueArray){
  valueArray = valueArray.filter(n=>n!==undefined);
  console.log("This is the value array " + valueArray);
  var highestVal = Math.max.apply(null, valueArray);
  console.log("This is the highest value " + highestVal)
  return highestVal;
 }

 function calcScore (scoreArray){

   var sum = scoreArray.reduce((a, b) => a + b, 0)
   console.log("The Sum is "+ sum);
   return sum;
 }

單選按鈕結構(值在 questionValue 數組中)

<div class="input-control">
          <input id="1" class="toggle" name="1" value=0 type="radio">
          <label for="1" class="btn"><span>0</span> I never take longer than 30 minutes to fall asleep.</label>
          <input id="2" class="toggle " name="1" value=1 type="radio">
          <label for="2" class="btn"><span>1</span> I take at least 30 minutes to fall asleep, less than half the time.</label>
          <input id="3" class="toggle" name="1" value=2 type="radio">
          <label for="3" class="btn"><span>2</span> I take at least 30 minutes to fall asleep, more than half the time.</label>
          <input id="4" class="toggle" name="1" value=3 type="radio">
          <label for="4" class="btn"><span>3</span> I take more than 60 minutes to fall asleep, more than half the time.</label>
        </div>

謝謝!

 const sumMax = (arr, ...pos) => pos.map(p => Math.max(...arr.slice(p[0], ++p[1]))).reduce((p, c) => p + c) const myArray = [1, 2, 3, 4, 5] console.log(sumMax(myArray, [0, 2], [3, 4]))


根據 OP 評論更新

使sumMax成為實時函數的一種方法是在每次添加新項目時更新主數組,並在更新后的數組上調用sumMax (這不是性能優化的解決方案,我們有時會計算冗余最大值和總和,請參閱動態編程)。 為此,我們使用閉包

 function liveSumMax (arr, ...pos) { // wrapper function const mainArray = arr const positions = pos const sumMax = (ar, ...ps) => ps.map(p => { let max = Math.max(...ar.slice(p[0], p[1] + 1)) max = max > -Infinity ? max : 0 return max }).reduce((p, c) => p + c) return (...items) => { // live function mainArray.push(...items) // update mainArray return sumMax(mainArray, ...positions) } } const myArray = [1, 2, 3, 4, 5] const sumMax = liveSumMax(myArray, [0, 2], [3, 9]) // returns a function console.log(sumMax()) // no new item console.log(sumMax(10, 20)) console.log(sumMax(30, 40, 50, 60))

您可以對數組進行切片,從中獲取總和和最大值。

 var add = (a, b) => a + b, array = [1, 2, 3, 4, 5], max = Math.max(array.slice(0, 3).reduce(add), array.slice(3, 5).reduce(add)); console.log(max);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM