首页 > 解决方案 > 缺少一个条件

问题描述

我有 3 个条件:

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

你能帮我吗 :)

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

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

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

标签: javascriptamazon-web-services

解决方案


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


推荐阅读