2016-10-28 17 views
0

最小値と最大値の2つの配列があります。私は最小値と最大値が一致すれば最大値を表示し、両方の値が一致しない場合にminとmaxの両方の値をそれぞれ表示するforeachループを作りたい。私は最小値と最大値のキーを一致させたい。私はどのように2つの値を比較するのか分からない。私はこの配列を持っていますJavaScriptの2つの配列の値を比較する方法

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); 
関連する問題