如何在对象数组中保留最新的对象



我正在节点后端处理一些数据,并返回以下数据结构,我想根据tag_id对其进行筛选,并根据时间返回数组中最新的对象。

{
"undefined": [
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T10:25:57.151Z",
"floor": "A-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T10:23:57.279Z",
"floor": "Nav Bridge Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 2,
"alias": "2Yong",
"time": "2021-06-26T10:25:57.151Z",
"floor": "A-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 2,
"alias": "2Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
],
"b_deck_ccr": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:25:56.551Z",
"floor": "A-Deck",
"zone_name_alias": "b_deck_ccr"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zones": [
"B Deck CCR",
"B Deck Fire Station"
],
"zone_name_alias": "b_deck_ccr"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:23:56.199Z",
"floor": "Nav Bridge Deck",
"zone_name_alias": "b_deck_ccr"
}
],
"b_deck_fire_station": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:25:56.551Z",
"floor": "A-Deck",
"zone_name_alias": "b_deck_fire_station"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zone_name_alias": "b_deck_fire_station"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:23:56.199Z",
"floor": "Nav Bridge Deck",
"zone_name_alias": "b_deck_fire_station"
}
]
}

上面的数据结构,我想缩小到下面的数据结构

{
"undefined": [
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 2,
"alias": "2Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
],
"b_deck_ccr": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zone_name_alias": "b_deck_ccr"
}
],
"b_deck_fire_station": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zone_name_alias": "b_deck_fire_station"
}
]
}

我尝试过各种分组和合并,但似乎还不能想出正确的组合键!

我的思考过程是:

  1. 获取对象中的每个数组
  2. 按tag_id查找数组中的对象并对其进行分组
  3. 循环遍历分组对象并返回具有最新时间戳的对象

我走对了吗?或者有人能提出替代方案吗?

问候,

作为一个变体。您可以循环遍历每个组的元素,并收集id中最后一个项目及其索引的统计信息。然后,您可以通过所收集的索引将所有组元素替换为最后一个组元素。

const data = {
"undefined": [
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T10:25:57.151Z",
"floor": "A-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 1,
"alias": "Yong",
"time": "2021-06-26T10:23:57.279Z",
"floor": "Nav Bridge Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 2,
"alias": "2Yong",
"time": "2021-06-26T10:25:57.151Z",
"floor": "A-Deck",
"in_zone": true,
"in_correct_zone": false
},
{
"tag_id": 2,
"alias": "2Yong",
"time": "2021-06-26T11:15:58.008Z",
"floor": "B-Deck",
"in_zone": true,
"in_correct_zone": false
},
],
"b_deck_ccr": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:25:56.551Z",
"floor": "A-Deck",
"zone_name_alias": "b_deck_ccr"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zones": [
"B Deck CCR",
"B Deck Fire Station"
],
"zone_name_alias": "b_deck_ccr"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:23:56.199Z",
"floor": "Nav Bridge Deck",
"zone_name_alias": "b_deck_ccr"
}
],
"b_deck_fire_station": [
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:25:56.551Z",
"floor": "A-Deck",
"zone_name_alias": "b_deck_fire_station"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T11:14:12.844Z",
"floor": "B-Deck",
"zone_name_alias": "b_deck_fire_station"
},
{
"tag_id": 10,
"alias": "Lay 1",
"time": "2021-06-26T10:23:56.199Z",
"floor": "Nav Bridge Deck",
"zone_name_alias": "b_deck_fire_station"
}
]
};
for (const group of Object.values(data)) {
const stat = {};
for (let i = 0; i < group.length; i++) {
const item = group[i];
if (stat[item.tag_id] === undefined || stat[item.tag_id].time < item.time) {
stat[item.tag_id] = { time: item.time, index: i };
}
}
const lasts = Object.values(stat).reduce(
(acc, item) => {
acc.push(group[item.index]);
return acc;
},
[]
);
group.length = 0;
group.push(...lasts);
}
console.log(data);

最新更新