doufu8588 2014-11-19 21:57
浏览 98
已采纳

从JS函数的数组中获取MIN和MAX值?

I want to use the MIN and MAX aht_value value from my json_encoded to use in my JavaScript function? How is this achievable?

thanks in advance.

Array:

[{
   "username":"OXGOR",
   "aht_value":"241",
   "station":"B20"
  }
  {
   "username":"AISAI2",
    "aht_value":"199",
   "station":"B21"
  },
  {
   "username":"CAPAP3",
   "aht_value":"NA",
   "station":"B10"
  }]

map.php - JS where everything will be output at the end

<script type="text/javascript">
$(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"GET",
                    url : "show_aht.php",
                    data:{  } , // do I need to pass data if im GET ting?
                    dataType: 'json',
                    success : function(data){
                        //going through all DIVs only once with this loop
                            for(var i = 0; i < data.length; i++) { // loop over results
                            var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
                            if(divForResult.length) { // if a div was found
                                divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
                            }//end if
                            }//end for
                    }//end success
                });//end ajax   
              });//end click
            });//end rdy

map.php - JS: I want to put the MIN and MAX aht_value here instead of 100 and 1800 but doesnt work

    //get MIN value from the array
    var min = Math.min.apply(null, a.map(function(x) {
        var n = parseFloat(x.aht_value);
        return isNaN(n) ? 9007199254740992 : n;
    }));

    //get MAX value from the array              
    var max = Math.max.apply(null, a.map(function(x) {
        var n = parseFloat(x.aht_value);
        return isNaN(n) ? 0 : n;
    }));

    //function for calculation of background color depending on aht_value               
    function conv(x){
        return Math.floor((x - 100) / (1800 - 100) * 255);
    }

    //function for background color
    function colorMe(v){
        return "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
    }
</script>

show.php: where I use json_encode

$result = array();
    foreach ($memo as $username => $memodata) {
    if (in_array($username, array_keys($user))) {
    // Match username against the keys of $user (the usernames) 
    $userdata = $user[$username];
    //if AHT is null give N/A as value
    if (is_null($memodata['aht_value'])) {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => 'NA',
                                             'station'  => $userdata['station']
                                            );
    }//end inner if 
    //else give the actual value of AHT without the decimals
    else {
        $result[] = array( 'username'  => $userdata['username'],
                           'aht_value' => substr($memodata['aht_value'],0,-3),
                           'station'   => $userdata['station']
                                            );
echo json_encode($result);
  • 写回答

4条回答 默认 最新

  • dongya9346 2014-11-19 22:10
    关注

    You can use array.map() in combination with array.apply() to make simple code to calculate the min and max values from your object array. In your case it's slightly more difficult because you have non-numeric values that need to be filtered-out.

    var a = [ 
      {
       "username":"OXGOR",
       "aht_value":"241",
       "station":"B20"
      },
      {
       "username":"AISAI2",
        "aht_value":"199",
       "station":"B21"
      },
      {
       "username":"CAPAP3",
       "aht_value":"NA",
       "station":"B10"
      }
    ];
    
    function minAHT(objArray) {
      return Math.min.apply(null, objArray.map(function(x) {
        var n = parseFloat(x.aht_value);
        return isNaN(n) ? 9007199254740992 : n;
      }));
    }
      
    function maxAHT(objArray) {
      return Math.max.apply(null, objArray.map(function(x) {
        var n = parseFloat(x.aht_value);
        return isNaN(n) ? 0 : n;
      }));
    }
    
    var min = minAHT(a);
    var max = maxAHT(a);
    
    alert("min: " + min + " max: " + max);

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

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作