逐行获取对象

时间:2016-10-13 12:38:44

标签: jquery

我有一个对象:

ListViewRenderer

我得到了最大值和最小值:

{
    "status": 200,
    "error": false,
    "msg": "Successful response",
    "data": {
        "horodate": [ "2016-10-13 00:00:00", "2016-10-13 00:30:00", ... ], 
        "value": [ 2609.479, 2390.026, 2320.394, 2276.602, 2247.151, ... ]
    }
}

但是当我从对象中检索min和max时,如何获得最小值和最大值的$.getJSON(url, function (data) { // ... var arr = Object.keys(data['data']['value']).map(function(key) { return data['data']['value'][key]; }); var min = Math.min.apply(null, arr); var max = Math.max.apply(null, arr); console.log(min); //2609.479 console.log(max); //2247.151

3 个答案:

答案 0 :(得分:3)

问题是您是否从数据中断开了最小值/最大值。因此,您需要一种不同的方式来获得最大最大值,或者您需要循环设置并匹配最小值/最大值。我会以不同的方式做到这一点。

我会循环设置并进行检查,而不是寻找最小值和最大值。这样你就可以做一个循环了。



var result = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
    "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}

function getMinMax(data) {
   var details = {  //store first index for min and max
       minV : data.value[0],
       maxV : data.value[0],
       minH : data.horodate[0],
       maxH : data.horodate[0]
     };
   return data.value.reduce( function (d, val, ind ){  //I used reduce, you can use a for loop or forEach
     if (d.minV > val) {  //see if we have a min
       d.minV = val;
       d.minH = data.horodate[ind];
     }
     else if (d.maxV < val) {  //see if we have a max
       d.maxV = val;
       d.maxH = data.horodate[ind];
     }
     
     return d;
   }, details );
  
  
}

var results = getMinMax(result.data);
console.log(results);
&#13;
&#13;
&#13;

由于OP认为它很复杂,所以只使用for循环而不是reduce。

&#13;
&#13;
var result = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
    "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}

function getMinMax(data) {
  var details = { //store first index for min and max
    minV: data.value[0],
    maxV: data.value[0],
    minH: data.horodate[0],
    maxH: data.horodate[0]
  };

  //loop over the values
  for (var ind = 0; ind < data.value.length; ind++) {
    var val = data.value[ind];
    if (details.minV > val) { //see if we have a min
      details.minV = val;
      details.minH = data.horodate[ind];
    } else if (details.maxV < val) { //see if we have a max
      details.maxV = val;
      details.maxH = data.horodate[ind];
    }
  }
  //return the results
  return details;
}

var results = getMinMax(result.data);
console.log(results);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以获取最小索引,然后获取与此索引对应的horodate

data = {
  "status": 200,
  "error": false,
  "msg": "Successful response",
  "data": {
    "horodate": [ "2016-10-13 00:00:00", "2016-10-13 00:30:00", "2016-10-13 01:00:00", "2016-10-13 01:30:00", "2016-10-13 02:00:00"], 
    "value": [ 2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
  }
}
var values_arr = data['data']['value']  
var horodates_arr = data['data']['horodate']  

var date_for_value = function(value){
  var min_index = values_arr.indexOf(value);
  return horodates_arr[min_index];  
}

var min = {'value': Math.min.apply(null, values_arr),
           'horodate': date_for_value(min['value'])}

var max = {'value': Math.max.apply(null, values_arr),
           'horodate': date_for_value(max['value'])}

console.log(min); //Object { value=2247.151,  horodate="2016-10-13 02:00:00"}
console.log(max); //Object { value=2609.479,  horodate="2016-10-13 00:00:00"}

答案 2 :(得分:0)

$(function() {
    var result = {
        status: 200,
        error: false,
        msg: "Successful response",
        data: {
            "horodate": ["2016-10-13 00:00:00", "2016-10-13 00:30:00", "x", "y", "z"],
            "value": [2609.479, 2390.026, 2320.394, 2276.602, 2247.151]
        }
    }

    var highest = 0; // for the highest value
    var lowest = Number.MAX_VALUE; // for the lowest value
    var lowIndex = 0; // for the lowest date
    var highndex = 0; // for the highest date
    var obj = {}; // object that hold all the info

    $(result.data.value)
        .each(function(i, v) {

            if (v > highest) {
                highest = v;
                highndex = i
            }

            if (v < lowest) {
                lowest = v;
                lowIndex = i
            }
           // if it is the last item in the array then set values
            if ((i + 1) == result.data.value.length) { 
                obj.highest = highest;
                obj.lowest = lowest;
                obj.dateHigh = result.data.horodate[highndex]
                obj.dateLow = result.data.horodate[lowIndex]
            }


        })
    console.log(obj);
});

fiddle