javascript - 如何按天获得最大值?
问题描述
这似乎是一个简单的问题,但我找不到一个好的解决方案。正如您在下面的片段中看到的,我按天和代码对数组进行分组。这是我想做的。但我有一条规则要执行:如果 A > 6 或 B > 60在一天中的任何时候(仅当代码是 GLLD008)那么 GLLD008 应该变成 GLLD0015(A 和 B 是 varsyscode)
输出应该是:
5 - Thu Sep 06 2018 16:03:10 GMT+0200 (CEST) - GLLD008 - A ( Because A <=6 & no B => GLLD008)
7 - Mon Sep 10 2018 13:32:28 GMT+0200 (CEST) - GLLD015 - A ( Because A > 6 => GLLD015)
85 - Wed Sep 05 2018 15:07:00 GMT+0200 (Central European Summer Time) - GLLD015 - B
如果有人可以帮忙
谢谢
var myArray = [
{
"value": 5,
"datetime": 1536242590000,
"code": "GLLD008",
"varsyscode": "A"
},
{
"value": 7,
"datetime": 1536579148000,
"code": "GLLD008",
"varsyscode": "A"
},
{
"value": 6,
"datetime": 1536579688000,
"code": "GLLD008",
"varsyscode": "A"
},
{
"value": 5,
"datetime": 1536833430000,
"code": "GLLD008",
"varsyscode": "A"
},
{
"value": 85,
"datetime": 1536152820000,
"code": "GLLD008",
"varsyscode": "B"
},
{
"value": 69,
"datetime": 1536154560000,
"code": "GLLD008",
"varsyscode": "B"
},
{
"value": 73,
"datetime": 1536571560000,
"code": "GLLD008",
"varsyscode": "B"
},
{
"value": 69,
"datetime": 1536575160000,
"code": "GLLD008",
"varsyscode": "B"
}
];
var helper = {};
// Group by day and code
var result = myArray.reduce(function(r, o) {
var day = new Date(o.datetime).getFullYear() + '/' + new Date(o.datetime).getMonth() + '/' + new Date(o.datetime).getDay();
var key = day + '-' + o.code;
if (!helper[key]) {
helper[key] = Object.assign({}, o);
r.push(helper[key]);
}
return r;
}, []);
result.forEach(function(element) {
console.log(element.value + " - " + (new Date(element.datetime).toString()) + " - " + element.code + " - " + element.varsyscode);
});
解决方案
您需要执行几个步骤才能更新code
.
- 检查是否
code
是"GLLD008"
- 检查是否
value
为数字。 - 评估
varsyscode
- 评估
value
- 更新
code
- 评估
您可以在生成key
.
if (o.code === 'GLLD008' && isNumeric(o.value)) {
switch (o.varsyscode) {
case 'A':
if (o.value > 6) {
o.code = 'GLLD0015'
}
break;
case 'B':
if (o.value > 60) {
o.code = 'GLLD0015'
}
break;
}
}
执行
var myArray = [
{ "value": 5, "datetime": 1536242590000, "code": "GLLD008", "varsyscode": "A" },
{ "value": 7, "datetime": 1536579148000, "code": "GLLD008", "varsyscode": "A" },
{ "value": 6, "datetime": 1536579688000, "code": "GLLD008", "varsyscode": "A" },
{ "value": 5, "datetime": 1536833430000, "code": "GLLD008", "varsyscode": "A" },
{ "value": 85, "datetime": 1536152820000, "code": "GLLD008", "varsyscode": "B" },
{ "value": 69, "datetime": 1536154560000, "code": "GLLD008", "varsyscode": "B" },
{ "value": 73, "datetime": 1536571560000, "code": "GLLD008", "varsyscode": "B" },
{ "value": 69, "datetime": 1536575160000, "code": "GLLD008", "varsyscode": "B" },
{ "value": "APRV", "datetime": 1536152820000, "code": "GLLD008", "varsyscode": "C" },
{ "value": "APRV", "datetime": 1536575040000, "code": "GLLD008", "varsyscode": "C" },
{ "value": "APRV", "datetime": 1536590700000, "code": "GLLD008", "varsyscode": "C" }
];
var helper = {};
// Group by day and code
var result = myArray.reduce((r, o) => {
var day = formatDate(o.datetime);
if (o.code === 'GLLD008' && isNumeric(o.value)) {
switch (o.varsyscode) {
case 'A':
if (o.value > 6) {
o.code = 'GLLD0015'
}
break;
case 'B':
if (o.value > 60) {
o.code = 'GLLD0015'
}
break;
}
}
var key = day + '-' + o.code;
if (!helper[key]) {
helper[key] = Object.assign({}, o);
r.push(helper[key]);
}
return r;
}, []);
var grouped = result.reduce((o, curr) => {
let key = formatDate(curr.datetime);
if (!isNumeric(curr.value)) {
return o; // skip non-numeric value e.g. "APRV"
}
if (o[key] == null) {
return Object.assign(o, { [key] : curr }); // store initial entry
}
let prev = o[key]; // get stored value for key
// If you wanted the max value... (this is overriden below)
if (curr.value > prev.value) {
o[key] = curr;
}
// If you only want to keep "GLLD008"
if (curr.code === 'GLLD008' && prev.code === 'GLLD0015') {
o[key] = curr;
}
return o;
}, {});
result = Object.keys(grouped).map(key => grouped[key]); // map to list
result.sort((a, b) => new Date(a.datetime) - new Date(b.datetime)); // sort list
result.forEach(entry => {
console.log([
entry.value, formatDate(entry.datetime), entry.code, entry.varsyscode
].join(' - '));
});
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function formatDate(date) {
return ((d) => d.getFullYear() + '/' + d.getMonth() + '/' + d.getDate())(new Date(date));
}
//Edited
推荐阅读
- oracle - Gitlab找不到工件com.oracle ojdbc8
- linux - 如何重命名目录中的多个文件,保留 Linux 中的扩展名
- java - 通过互联网向带有 MAC 地址的安卓设备发送短信
- java - 使用 keycloak 保护后无法使用有效的 keycloak 令牌访问 REST 服务
- entity-framework - 使用子实体上的条件进行一对多(父实体和子实体)Linq
- javascript - findById 比 findOne({userId: userId}) 快吗?
- java - 将 .txt 文件中的双精度读入数组列表
- python-3.x - 将字符串中的单个字符转换为大写
- python - Python PyPDF2 编写器不适用于解密
- javascript - 使用 Promise 顺序加载脚本