javascript - 缺少一个条件
问题描述
我有 3 个条件:
如果大多数平均值等于 200,则将“向上”显示到数组中
如果大多数平均值与 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 天前]
预期结果(最后一个条件)(/!\ 第二个区域的平均变化值)
- ['向上','向上']
- [空,3 天前]
解决方案
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);
推荐阅读
- realm - 领域通知导致异常:对象已被删除或无效
- javascript - 使用 flexbox 在容器中动态调整盒子
- r - 按组 ID 变量分组的单个变量的条形图,以实现一个条形图,其中包含每个特定组的所有值
- facebook - 为什么 Graph API 会跳过提要帖子?
- python - 如何使用日期作为 x 轴绘制数据框
- asp.net-mvc - 通过 HTTPS 测试 Stripe webhook 不断失败:超时
- c# - 如何在 C# 中使用 WordNet?
- android - 当滚动底部导航栏不隐藏时 - BottomNavigationBehavior
- hta - 使用 IE10 未显示 HTA 图标
- asp.net-mvc - godaddy 在我网站的 URL 中添加了我的文件夹名称