dtn913117 2016-10-28 07:11
浏览 92
已采纳

如何在javascript中比较两个数组的值

I have two array with minimum and maximum value. I want to make foreach loop where if minimum and maximum values matches then only display maximum value and if both the values don't match then display both the values min and max respectively. I want to match the key of min and max values. I am not getting how do i compare two values. I have this array

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

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

This is the array value which i get.

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"]

I want to achieve same thing but in 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"];

}
        }


    ?>
  • 写回答

2条回答 默认 最新

  • doulian7305 2016-10-28 07:21
    关注

    You could iterate the arrays, make a comparison and return the combined result in a new array.

    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)

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行