gpt4 book ai didi

javascript - 遍历大型数据集的最佳方式

转载 作者:行者123 更新时间:2023-11-29 10:56:09 25 4
gpt4 key购买 nike

我有一个很大的 JSON 值数据集。以下是该 JSON 对象的一个​​小快照。

 {
"response": [
{
"data": [
{
"value": 1,
"minute": "2019-06-10 11:51",
"action": "firstApp",
},
{
"value": 10,
"minute": "2019-06-10 11:51",
"action": "secondApp",
},
{
"value": 100,
"minute": "2019-06-10 11:51",
"action": "thirdApp",
},
{
"value": 10,
"minute": "2019-06-10 11:52",
"action": "firstApp",
},
{
"value": 20,
"minute": "2019-06-10 11:52",
"action": "secondApp",
},
{
"value": 115,
"minute": "2019-06-10 11:52",
"action": "thirdApp",
}, ]
}]
}

现在考虑数据数组中大约有 800 个这样的项目。我想做的是创建一个 JSON 对象,它对每个时间图都有值,就像下面的一样

[
{
"timestamp" : "2019-06-10 11:51",
"firstApp" : {
"value" : 1,
},
"secondApp": {
"value":10,
},
"thirdApp": {
"value" : 100,
}
},
{
"timestamp" : "2019-06-10 11:52",
"firstApp" : {
"value" : 10
},
"secondApp": {
"value":20,
},
"thirdApp": {
"value" : 115,
}
}
]

我已经编写了以下代码,但它花费了很多时间(大约 10-12 秒)(这是意料之中的) 请参阅此 CodeSandbox ( https://codesandbox.io/s/objective-bartik-0r30j?autoresize=1&expanddevtools=1&fontsize=14&hidenavigation=1&module=%2Fsrc%2Findex.js) 链接以查看代码的运行情况。

我面临的问题是需要花费大量时间(理所当然地)

  1. 数据集很大
  2. 我的代码很糟糕——因为到处都是循环

我无法控制 1 但我绝对可以控制 2 。你能给我一些关于如何解决这个问题的想法吗?

更新这是性能选项卡的屏幕截图[![在此处输入图片描述][1]][1]

谢谢

let data = {
response: [
{
data: [
{
value: 1,
minute: "2019-06-10 11:51",
action: "firstApp"
},
{
value: 10,
minute: "2019-06-10 11:51",
action: "secondApp"
},
{
value: 100,
minute: "2019-06-10 11:51",
action: "thirdApp"
},
{
value: 10,
minute: "2019-06-10 11:52",
action: "firstApp"
},
{
value: 20,
minute: "2019-06-10 11:52",
action: "secondApp"
},
{
value: 115,
minute: "2019-06-10 11:52",
action: "thirdApp"
}
]
}
]
};

function massageData(data) {
let historyData = [];
let uniqueTimeStamps = [];
let event = {
timestamp: "",
firstApp: {
value: 0
},
secondApp: {
value: 0
},
thirdApp: {
value: 0
}
};

for (var i = 0; i < data.length; i++) {
let item = data[i];

if (item.minute) {
if (!uniqueTimeStamps.includes(item.minute)) {
let timestamp = item.minute;
console.log("--------------------");
console.log(timestamp);
event.timestamp = timestamp;
event.firstApp.value = getDataValue(data, timestamp, "firstApp");
event.secondApp.value = getDataValue(data, timestamp, "secondApp");
event.thirdApp.value = getDataValue(data, timestamp, "thirdApp");
console.log(event);
historyData.push(event);
uniqueTimeStamps.push(item.minute);
}
}
}

return historyData;
}

function getDataValue(data, timestamp, action) {
for (var i = 0, len = data.length; i < len; i++) {
let item = data[i];
console.log(item);
if (item["minute"] === timestamp && item["action"] === action) {
return parseInt(item["value"]);
}
}
}

let workData = data.response[0].data;
let formattedData = massageData(workData);
console.log(formattedData);

最佳答案

您可以为相同的时间戳采用经典哈希表,并使用哈希表更新值。

function massageData(data) {
var historyData = [],
hash = Object.create(null),
i, item;

for (i = 0; i < data.length; i++) {
item = data[i];
if (!hash[item.minute]) {
historyData.push(hash[item.minute] = { timestamp: item.minute, firstApp: { value: 0 }, secondApp: { value: 0 }, thirdApp: { value: 0 } });
}
hash[item.minute][item.action].value = item.value;
}
return historyData;
}

var data = { response: [{ data: [{ value: 1, minute: "2019-06-10 11:51", action: "firstApp" }, { value: 10, minute: "2019-06-10 11:51", action: "secondApp" }, { value: 100, minute: "2019-06-10 11:51", action: "thirdApp" }, { value: 10, minute: "2019-06-10 11:52", action: "firstApp" }, { value: 20, minute: "2019-06-10 11:52", action: "secondApp" }, { value: 115, minute: "2019-06-10 11:52", action: "thirdApp" }] }] },
workData = data.response[0].data,
formattedData = massageData(workData);

console.log(formattedData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 遍历大型数据集的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com