从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);

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>

Since you are already iterating the result set in PHP, I would suggest a couple of options. You can either sort the array in PHP beofre returning, such that the min value can be derived in js by looking at arrayFromJson[0].aht_value and max by arrayFromJson[arrayFromJson.length - 1].aht_value. Or you can collect the min.max value when iterating the array and send them as additional data in your JSON such that you JSON might look like

{
   aht_min: XXX,
   aht_max: XXX,
   records: [
       {
           "username":"OXGOR",
           "aht_value":"241",
           "station":"B20"
       },
       ...
   ]
}

Of course, you can always iterate through the array that is derived from JSON in javascript to determine the min/max values, but that seem like unnecessary overhead if you just do this on the server.

You can capture the min and max in the same go to save some execution time there. If this kind of thing needs to be done quite a bit or in multiple places it's probably better to do it on the backend as Mike suggests.

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

var minMax = function(dataSet) {
  var min = null, max = null, i, len;
  for (i = 0, len = dataSet.length; i < len; ++i) {
    var data = dataSet[i], value = parseInt(data.aht_value);
    if (isNaN(value)) {
      continue;
    }

    if (min == null || value < min) {
      min = value;
    }
    if (max == null || value > max) {
      max = value;
    }
  }
  return {min: min, max: max};
};

alert(JSON.stringify(minMax(data)));

</div>

pass json object to maxVal and minVal function. In the success function you can use data as:

function maxVal(data) {  
  var max, k; // don't set max=0, because keys may have values < 0  
  //go through single key
  for (var key in data) { if (data.hasOwnProperty(key)) { max = parseInt(data[key]); break; }} 
  //go through all keys
  for (var key in data) { if (data.hasOwnProperty(key)) { if((k = parseInt(data[key])) > max) max = k; }}  

var min, m; // don't set max=0, because keys may have values < 0  
  //go through single key
  for (var key in data) { if (data.hasOwnProperty(key)) { min = parseInt(data[key]); break; }} 
  //go through all keys
  for (var key in data) { if (data.hasOwnProperty(key)) { if((m = parseInt(data[key])) < min) min = m; }}  

//finally both max and min has maximum and minimum value 
} 

OR you can sort and get first and last object by key val

function sortByAttribue(d, attribute) {
  return d.sort(function(a,b) { 
    return a[attribute] < b[attribute];
  });
}

sortByAttribue(data, "aht_value")

Now get first value of key "aht_value" FROM first object and last object