[英]how to compare values of two array in javascript
我有两个数组的最小值和最大值。 我想在foreach循环中,如果最小值和最大值匹配,则仅显示最大值,如果两个值都不匹配,则分别显示min和max值。 我想匹配最小值和最大值的键。 我没有得到如何比较两个值。 我有这个数组
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"];
}
}
?>
您可以迭代数组,进行比较,然后将合并的结果返回到新数组中。
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)
尝试这个,
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.