gpt4 book ai didi

javascript - 对 JSON 数据进行排序并获取前 n 条记录

转载 作者:行者123 更新时间:2023-12-01 01:40:41 26 4
gpt4 key购买 nike

我有大约数百万条记录的 json 数据。我必须做一个简单但棘手的功能。

What I have to do?

我必须从该 json 中收集出现次数最多的前 10 个项目及其计数。出现最多的意思是 json 中项目的最多数量。我还不确定,我将如何使用 count,我的意思是我可以将它添加到与属性相同的 json 对象中。

Here is what I did so far.

//my origional json, it's too big but adding some portion of it.
var jsonData = [
{
"id": "5",
"name": "#5"
},
{
"id": "1",
"name": "#1"
},
{
"id": "2",
"name": "#2"
},
{
"id": "8",
"name": "#8"
},
{
"id": "1",
"name": "#1"
},
{
"id": "10",
"name": "#10"
},
{
"id": "2",
"name": "#2"
}];

var top10Data = [];

//geting top 10 items
function getTop10Data() {
var i = 0;
while (i <= 20) {
top10Data.push(getTop1Data());
i++;
}

return true;
}

//getting top 1 data that has max count in json
function getTop1Data() {
var store = jsonData, distribution = {}, max = 0, result = [];

store.forEach(function (a) {
distribution[a] = (distribution[a] || 0) + 1;
if (distribution[a] > max) {
max = distribution[a];
result = [a];
return;
}
if (distribution[a] === max) {
result.push(a);
}
});

//remove this item with it's all occurences, and push it to top10Data
removeData(result);

return result;
}

//remove items from origional json. but this is not working properly as it removes only one item from top
function removeData(result) {
var length = jsonData.length;
for (var i = 0; i < length; i++) {
if (jsonData[i].toppings === result[0].toppings) {
jsonData.splice(jsonData[i], 1);
}
}
}

My question.

我认为我的做法不正确,是否有更好的方法来处理这种情况。如果我的方法没问题,我在当前代码中缺少什么。

任何帮助将不胜感激。

最佳答案

您可以将数据减少到一个对象中,该对象保存每个项目的计数,并按字符串化项目进行索引。然后,如果没有相当数量的唯一对象,您可以按条目的出现次数对条目进行排序,然后对前 10 个进行切片。

var jsonData = [    
{
"id": "5",
"name": "#5"
},
{
"id": "1",
"name": "#1"
},
{
"id": "2",
"name": "#2"
},
{
"id": "8",
"name": "#8"
},
{
"id": "1",
"name": "#1"
},
{
"id": "10",
"name": "#10"
},
{
"id": "2",
"name": "#2"
}];
const counts = jsonData.reduce((a, obj) => {
const string = JSON.stringify(obj);
a[string] = (a[string] || 0) + 1
return a;
}, {});
const result = Object.entries(counts)
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([string, count]) => ({ count, obj: JSON.parse(string) }));
console.log(result);

要将计数添加到原始数据,请在构造 counts 对象后迭代数据:

var jsonData = [    
{
"id": "5",
"name": "#5"
},
{
"id": "1",
"name": "#1"
},
{
"id": "2",
"name": "#2"
},
{
"id": "8",
"name": "#8"
},
{
"id": "1",
"name": "#1"
},
{
"id": "10",
"name": "#10"
},
{
"id": "2",
"name": "#2"
}];
const counts = jsonData.reduce((a, obj) => {
const string = JSON.stringify(obj);
a[string] = (a[string] || 0) + 1
return a;
}, {});
jsonData.forEach((item) => {
item.count = counts[JSON.stringify(item)];
});
console.log(jsonData);

关于javascript - 对 JSON 数据进行排序并获取前 n 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52438488/

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