我想使用我的aht_value
中的MIN和MAX json_encoded
值来在我的JavaScript函数中使用?这怎么可以实现?
提前感谢。
数组:
[{
"username":"OXGOR",
"aht_value":"241",
"station":"B20"
}
{
"username":"AISAI2",
"aht_value":"199",
"station":"B21"
},
{
"username":"CAPAP3",
"aht_value":"NA",
"station":"B10"
}]
map.php - JS将在最后输出所有内容
<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:我想把MIN和MAX aht_value
放在这里而不是100和1800但是不起作用
//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
:我在哪里使用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);
答案 0 :(得分:1)
您可以将array.map()与array.apply()结合使用,生成简单的代码来计算对象数组中的最小值和最大值。在你的情况下,它有点困难,因为你有非数字值需要过滤掉。
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);
答案 1 :(得分:0)
由于您已经在PHP中迭代结果集,我建议使用几个选项。您可以在返回的PHP中对数组进行排序,以便通过查看arrayFromJson[0].aht_value
和最大arrayFromJson[arrayFromJson.length - 1].aht_value
来在js中导出最小值。或者,您可以在迭代数组时收集min.max值,并将它们作为JSON中的其他数据发送,以便JSON可能看起来像
{
aht_min: XXX,
aht_max: XXX,
records: [
{
"username":"OXGOR",
"aht_value":"241",
"station":"B20"
},
...
]
}
当然,你总是可以在javascript中遍历从JSON派生的数组来确定最小/最大值,但如果你只是在服务器上执行此操作,这似乎是不必要的开销。
答案 2 :(得分:0)
您可以同时捕获最小值和最大值以节省一些执行时间。如果这种事情需要在相当多的地方或在多个地方完成,那么最好像Mike建议的那样在后端进行。
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)));
答案 3 :(得分:0)
将json对象传递给maxVal和minVal函数。 在成功功能中,您可以将数据用作:
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 您可以按键val排序并获取第一个和最后一个对象
function sortByAttribue(d, attribute) {
return d.sort(function(a,b) {
return a[attribute] < b[attribute];
});
}
sortByAttribue(data, "aht_value")
现在从第一个对象和最后一个对象
获取键“aht_value”的第一个值