I have below algorithmic base problem and I'm trying to solve it with ES6, but I'm getting confused with groupBy
,(我遇到了以下基于算法的基本问题,并且尝试使用ES6进行解决,但是我对groupBy
感到困惑,)
let data = [
{id: "2", time: "3/11/2016 02:02:58", value: 7.0},
{id: "1", time: "3/11/2016 02:12:32", value: 6.5},
{id: "1", time: "3/11/2016 02:13:11", value: 7.25},
{id: "4", time: "3/11/2016 02:13:54", value: 8.75},
{id: "2", time: "3/11/2016 05:02:45", value: 11.0},
{id: "4", time: "3/11/2016 06:32:42", value: 5.0},
{id: "2", time: "3/11/2016 06:35:12", value: 2.0},
{id: "1", time: "3/11/2016 06:45:01", value: 12.0},
{id: "1", time: "3/11/2016 06:59:59", value: 11.75},
{id: "2", time: "3/11/2016 07:01:53", value: 1.0},
{id: "1", time: "3/11/2016 07:02:54", value: 4.5},
{id: "3", time: "3/11/2016 07:02:54", value: 15.75},
{id: "6", time: "3/11/2016 07:02:54", value: 14.25},
{id: "2", time: "3/11/2016 07:03:15", value: 12.0},
{id: "2", time: "3/11/2016 08:02:22", value: 3.0},
{id: "2", time: "3/11/2016 09:41:50", value: 4.0},
{id: "2", time: "3/11/2016 10:02:54", value: 5.0},
{id: "2", time: "3/11/2016 11:05:35", value: 10.0},
{id: "2", time: "3/11/2016 13:02:21", value: 6.0},
{id: "5", time: "3/11/2016 13:02:40", value: 8.0},
{id: "4", time: "3/11/2016 13:02:55", value: 8.0},
{id: "5", time: "3/11/2016 13:33:34", value: 8.0},
{id: "5", time: "3/11/2016 13:42:24", value: 8.0},
{id: "5", time: "3/11/2016 13:47:44", value: 6.25},
{id: "5", time: "3/11/2016 14:02:54", value: 4.25},
{id: "5", time: "3/11/2016 14:03:04", value: 5.25},
{id: "5", time: "3/11/2016 15:12:55", value: 6.25},
{id: "2", time: "3/11/2016 16:02:36", value: 8.0},
{id: "5", time: "3/11/2016 16:22:11", value: 8.5},
{id: "5", time: "3/11/2016 17:18:19", value: 11.25},
{id: "5", time: "3/11/2016 18:19:20", value: 9.0},
{id: "2", time: "3/11/2016 23:59:59", value: 9.0}
];
Trying to get subset of array with the following condition:(尝试获取具有以下条件的数组子集:)
Each id
within each one hour period, only the most expensive value should be in result set.(在每个一小时内的每个id
,结果集中应该只包含最昂贵的值。)
If more than one object with the same id
equals the most expensive value in a one hour period, only place the earliest one.(如果一个以上具有相同id
对象等于一小时内最昂贵的值,则仅放置最早的一个。)
If there are more than 8 objects for an id
in the overall array of objects, remove it.(如果整个对象数组中一个id
对象超过8个,则将其删除。)
I know how it's easily achievable in SQL with MAX on 'value' and GroupBy on 'time' column, but here I get confused with the JS loop.(我知道如何在SQL中使用“值”上的 MAX和“时间”列上的 GroupBy轻松实现,但是在这里,我对JS循环感到困惑。) Please suggest some tricks if possible.(如果可能,请提出一些技巧。)
I was trying like below, but seems still long way to go(我在尝试以下方法,但似乎还有很长的路要走)
var newArr = [];
data.forEach(function (el) {
var findIndex = newArr.findIndex(function (item) {
return item.time === el.time;
});
if (findIndex === -1) {
newArr.push(el);
} else if (el.value > newArr[findIndex].value) {
newArr[findIndex].value = el.value;
newArr[findIndex].time = el.time;
} else {
newArr[findIndex].time = el.time;
}
});
just for making it more clear output will be like that(只是为了使输出更加清晰)
[
{ id: "4",time: "3/11/2016 02:13:54",value: 8.75 },
{ id: "1", time: "3/11/2016 06:45:01",value: 12.0 },
{ id: "3",time: "3/11/2016 07:02:54",value: 15.75 },
{ id: "4", time: "3/11/2016 13:02:55", value: 8.0}
]
Record of id: "2" and "5" removed because it has more than 8 appearance I found something here but seems ruby(id的记录:“ 2”和“ 5”已删除,因为它的外观超过了8个,我在这里找到了一些东西,但看起来是红宝石)
ask by freedom translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…