2016-10-28 55 views
0

我有兩個數組,最小值和最大值。我想做foreach循環,如果最小值和最大值匹配,那麼只顯示最大值,如果兩個值不匹配,則分別顯示最小值和最大值。我想匹配最小值和最大值的關鍵。我沒有得到如何比較兩個值。我有這個陣列如何在javascript中比較兩個數組的值

var interestRateMin = []; 
data.forEach(function(element){ 
    this.push(element.interestRateMin); 
}, interestRateMin); 

var interestRateMax = []; 
data.forEach(function(element){ 
    this.push(element.interestRateMax); 
}, interestRateMax); 

這是我得到的數組值。

MinRate = ["10.0", "11.5", "12.0", "12.0", "12.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "13.5", "13.75", "14.0", "14.0"] 

MaxRate = ["10.0", "11.75", "12.0", "12.0", "24.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "17.0", "13.75", "14.0", "14.0"] 

我想達到同樣的事情,但在JavaScript中。

<?php 
     foreach ($json['resultList'] as $key=>$value) { 
      if($json["resultList"][$key]["MinRate"] == $json["resultList"][$key]["MaxRate"]){ 
       $interest = $json["resultList"][$key]["MinRate"]; 
      } 
      else{ 
       $interest = $json["resultList"][$key]["MinRate"].' - '.$json["resultList"][$key]["MaxRate"]; 

} 

      if($json["resultList"][$key]["MinPercentage"] == $json["resultList"][$key]["MaxPercentage"]){ 
       $financing = $json["resultList"][$key]["MinPercentage"]; 
      } 
      else{ 
       $financing = $json["resultList"][$key]["MinPercentage"].' - '.$json["resultList"][$key]["MaxPercentage"]; 

} 
     } 


    ?> 
+0

請加'只顯示想要的結果...'。這怎麼發生? –

回答

0

您可以迭代數組,進行比較並將組合結果返回到新數組中。

var minRate = ["10.0", "11.5", "12.0", "12.0", "12.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "13.5", "13.75", "14.0", "14.0"], 
 
    maxRate = ["10.0", "11.75", "12.0", "12.0", "24.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "17.0", "13.75", "14.0", "14.0"], 
 
    result = minRate.map(function (a, i) { 
 
     return a === maxRate[i] ? a : a + ' - ' + maxRate[i]; 
 
    }); 
 

 
console.log(result)

+0

謝謝你的工作 – user6924814

0

試試這個,

var MinRate = ["10.0", "11.5", "12.0", "12.0", "12.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "13.5", "13.75", "14.0", "14.0"], 
     MaxRate = ["10.0", "11.75", "12.0", "12.0", "24.0", "12.0", "12.0", "12.25", "12.5", "12.75", "12.75", "13.0", "13.0", "13.25", "17.0", "13.75", "14.0", "14.0"] 
    result = MinRate.map(function (a, i) { 
     return a === MaxRate[i] ? a : (a - MaxRate[i]); 
    }); 
    console.log(result);