我有3个条件:
如果大多数平均值等于200,则将“ up”显示在数组中
如果大多数平均值不等于200,则将“向下”显示到数组中,并采用第一个时间戳,平均值不等于200。
我不知道怎么做最后一个:
能帮我一下吗:)
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);
});
实际结果(第一个和第二个条件):
预期结果(最后一个条件)(/!\第二个区域的平均变化值)
答案 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);