循环遍历对象的问题

时间:2018-05-02 19:12:18

标签: javascript loops object

我有从HTML表创建的对象,每个对象都是所述表中的一行。它们看起来像这样:

function rowObj(aName,suctionTemp,liquidTemp,outsideTemp,boxTemp,foodTemp1,foodTemp2,foodTemp3,foodTemp4,suctionPsi,liquidPsi,
            compressorAmps,condensorFan,evaporatorFan,mainPower,doorSwitch,input2,input3,input4,hour,minute,amPm){
this.aName=aName,
    this.suctionTemp=suctionTemp,
    this.liquidTemp=liquidTemp,
    this.outsideTemp=outsideTemp,
    this.boxTemp=boxTemp,
    this.foodTemp1=foodTemp1,
    this.foodTemp2=foodTemp2,
    this.foodTemp3=foodTemp3,
    this.foodTemp4=foodTemp4,
    this.suctionPsi=suctionPsi,
    this.liquidPsi=liquidPsi,
    this.compressorAmps=compressorAmps,
    this.condensorFan=condensorFan,
    this.evaporatorFan=evaporatorFan,
    this.mainPower=mainPower,
    this.doorSwitch=doorSwitch,
    this.input2=input2,
    this.input3=input3,
    this.input4=input4,
    this.hour=hour,
    this.minute=minute,
    this.amPm=amPm;

};

我要做的是循环浏览这些对象并获取单位所在的时间。我可以通过以下方式做到这一点:

var minuteArray=[];
for ( var key in objects){
    if (objects[key].compressorAmps > 0 && objects[key].condensorFan > 0){
        minuteArray.push(objects[key].minute);
    }else{
        key++;
        break;
    }
}
var firstMinute= minuteArray.shift();
var lastMinute=minuteArray.pop();

var hourArray=[];
for ( var key in objects){
    if (objects[key].compressorAmps > 0 && objects[key].condensorFan > 0){
        hourArray.push(objects[key].hour);
    }else{
        key++;
        break;
    }
}
var firstHour= hourArray.shift();
var lastHour=hourArray.pop();

在这里,我将所有值都推送到一个数组中,然后取出第一个和最后一个元素并从中获取小时和分钟。这允许我计算它运行了多长时间。这也只是给了我当前的运行时间。最终我想: 1.将每组运行值存储在一个数组中 2.然后是数组中等于0时的值 3.然后继续存储并存储下一组,其中> 0,在不同的数组

    for ( key in objects){
    if ( objects[key].compressorAmps > 0 && objects[key].condensorFan > 0){
        //store values in an array until compressorAmps === 0, skip those
        //then store the next set, where compressorAmps >0 in a different array
    }else if (objects[key].compressorAmps === 0 && objects[key].condensorFan === 0){

    }
}

我不知道这是否可能,如果是的话,我很难搞清楚。任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

将值放在按时间排序的数组中(我假设这是你想要的)。然后你需要做的就是迭代你的数组,测试每个值并寻找边缘条件。如下所示:

var result = [];    // this will be a 2D array with your results
var curr = null;    // this is the current run
for (var i=0; i < data.length; i++) {
     if (data[i].compressorAmps > 0) {   // test condition, change as needed
         if (!curr) {                    // if we don't have a run, start one
             curr = [];
         }
         curr.push(data[i]);             // store the current value
     } else {
        if (curr) {                      // if this is the end of a run
            result.push(curr);           // save the run
            curr = null;
        }
     }
}

现在您可以遍历result并获取每次运行的第一个和最后一个来计算时间:

for (var i=0; i < result.length; i++) {
    var start = result[i][0];
    var end = result[i][result[i].length - 1];
    console.log("Run started at: " + start.hour + ":" start.minute);
    console.log("Run ended at: " + end.hour + ":" end.minute);
}