缺少条件

时间:2019-01-18 15:54:24

标签: javascript amazon-web-services

我有3个条件:

  • 如果大多数平均值等于200,则将“ up”显示在数组中

  • 如果大多数平均值不等于200,则将“向下”显示到数组中,并采用第一个时间戳,平均值不等于200。

我不知道怎么做最后一个:

  • 我们再次编译脚本,如果在我按下“ down”键之前,现在大多数是200(“ up”键),那么我们必须保留时间戳,并且不要将其设置为null。

能帮我一下吗:)

getAllMetrics(regions).then(res => {
console.log(res);
/*
[[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:26:00.000, Average: 200},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}],
[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 400},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 400},
{Timestamp: 2019-01-15T08:26:00.000, Average: 400},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}]] */

/*    SECOND COMPILATION 
[[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:26:00.000, Average: 200},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}],
[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 400},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:26:00.000, Average: 200},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}]] */
tabRES = [];
//Loop regions
var i = -1;
TsTAB = [null, null, null];

var a=-1;
while( tabReg[++a] ){
    TsTAB.push( null );
}
while (res[++i]) {
    Avg = {
    up: 0,
    down: 0
    };
    Ts = "";
    RespARRAY = res[i];
    var j = -1;
    while (RespARRAY[++j]) {
    if (RespARRAY[j].Average == 200) {
        Avg.up++ // IF 200 -> UP
        //and push array TS
    } else {
        Avg.down++ // -> DOWN
        //Retrieve the last 
        Ts = Ts || RespARRAY[j].Timestamp;
    }
    }
    if (Avg.up > Avg.down) {
    tabRES.push('up');
    } else {
    tabRES.push('down');
    TsTAB[i] = TsTAB[i] || Ts;
    }
}
console.log(tabRES);
console.log(TsTAB);
}).catch(err => {
console.log(err);
}); 

实际结果(第一个和第二个条件):

  • ['上','下']
  • [null,3天前]

预期结果(最后一个条件)(/!\第二个区域的平均变化值)

  • ['up','up']
  • [null,3天前]

1 个答案:

答案 0 :(得分:0)

var res =  [[{Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
{Timestamp: "2019-01-15T08:32:00.000Z", Average: 200},
{Timestamp: "2019-01-15T08:26:00.000", Average: 200},
{Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
{Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}],
[{Timestamp: "2019-01-15T08:26:00.000Z", Average: 400},
{Timestamp: "2019-01-15T08:32:00.000Z", Average: 400},
{Timestamp: "2019-01-15T08:26:00.000Z", Average: 400},
{Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
{Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}]];

/*    SECOND COMPILATION 
[[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:26:00.000, Average: 200},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}],
[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 400},
{Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:26:00.000, Average: 200},
{Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
{Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}]] */

let results = res.reduce((acc,resArray) => {
                innerResult = resArray.reduce((_acc,obj) => {
                   if(obj.Average == 200) {
                      _acc.up++;
                   } else {
                      _acc.down++;
                      _acc.time = obj.Timestamp;
                   }
                   return _acc;
                }, { up:0, down:0, time:null });
                
                if(innerResult.up > innerResult.down) {
                  acc.res.push('up');
                  acc.ts.push(null);
                }
                else {
                  acc.res.push('down');
                  acc.ts.push(innerResult.time);
                }
                return acc;
              }, {res:[], ts:[]});
console.log(results.res);
console.log(results.ts);