我有一个平面 JSON 数组,其中 identifier
、categoryId
和 category
重复:
data: [
{
"identifier": "data",
"categoryId": "1",
"category": "Baked goods",
"product": "Aunt Hattie's",
"price": "375"
},
{
"identifier": "data",
"categoryId": "1",
"category": "Baked goods",
"product": "Back to Nature",
"price": "343"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Mars Muffin (McVitie's)",
"price": "465"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "McVitie's",
"price": "251"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Mr Kipling",
"price": "260"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Mr Kipling Frosty Fancies",
"price": "210"
},
{
"identifier": "data",
"categoryId": "3",
"category": "Dairy products",
"product": "Amul",
"price": "474"
},
{
"identifier": "data",
"categoryId": "3",
"category": "Dairy products",
"product": "Borden",
"price": "184"
},
{
"identifier": "data",
"categoryId": "3",
"category": "Dairy products",
"product": "Broughton Foods Company",
"price": "43"
},
]
如何计算每个类别的价格总计,并将对象插入每个类别的末尾,并将标识符
作为Total
与总值(value)一起插入。我知道将对象插入 JSON 数组,将对象推到最后。但这很好,因为我可以使用 sort
函数对数组进行排序,如下所示:
function compare(a,b) {
if (a.categoryId < b.categoryId)
return -1;
if (a.categoryId> b.categoryId)
return 1;
return 0;
}
data.sort(compare);
已编辑
我希望输出是这样的:
: [
{
"identifier": "data",
"categoryId": "1",
"category": "Baked goods",
"product": "Aunt Hattie's",
"price": "110"
},
{
"identifier": "data",
"categoryId": "1",
"category": "Baked goods",
"product": "Back to Nature",
"price": "344"
},
{
"identifier": "Total",
"categoryId": "1",
"category": "Baked goods",
"price": "454"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Mars Muffin (McVitie's)",
"price": "455"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Secret Recipe",
"price": "471"
},
{
"identifier": "data",
"categoryId": "2",
"category": "Cakes",
"product": "Vimto Jam Tarts",
"price": "235"
},
{
"identifier": "Total",
"categoryId": "2",
"category": "Cakes",
"price": "1161"
},
{
"identifier": "data",
"categoryId": "3",
"category": "Dairy products",
"product": "Alta Dena",
"price": "158"
},
{
"identifier": "data",
"categoryId": "3",
"category": "Dairy products",
"product": "Chivers",
"price": "399"
},
{
"identifier": "Total",
"categoryId": "3",
"category": "Dairy products",
"price": "557"
}
]
最佳答案
您可以使用哈希表来引用相同的类别,并为每个总组添加另一个对象。
在数据
末尾,现在包含所有总计。
要将数据排序到每个类别的顶部,将总计排序到底部,您可以添加总计检查,并将此部分移到类别部分的末尾。
var data = [{ identifier: "data", categoryId: "1", category: "Baked goods", product: "Aunt Hattie's", price: "375" }, { identifier: "data", categoryId: "1", category: "Baked goods", product: "Back to Nature", price: "343" }, { identifier: "data", categoryId: "2", category: "Cakes", product: "Mars Muffin (McVitie's)", price: "465" }, { identifier: "data", categoryId: "2", category: "Cakes", product: "McVitie's", price: "251" }, { identifier: "data", categoryId: "2", category: "Cakes", product: "Mr Kipling", price: "260" }, { identifier: "data", categoryId: "2", category: "Cakes", product: "Mr Kipling Frosty Fancies", price: "210" }, { identifier: "data", categoryId: "3", category: "Dairy products", product: "Amul", price: "474" }, { identifier: "data", categoryId: "3", category: "Dairy products", product: "Borden", price: "184" }, { identifier: "data", categoryId: "3", category: "Dairy products", product: "Broughton Foods Company", price: "43" }],
i,
l = data.length,
hash = Object.create(null),
categoryId, category, price;
for (i = 0; i < l; i++) {
({ categoryId, category, price } = data[i]);
if (!(categoryId in hash)) {
hash[categoryId] = { identifier: "total", categoryId, category, price: 0 };
data.push(hash[categoryId]);
}
hash[categoryId].price += +price;
}
data.sort((a, b) =>
a.categoryId - b.categoryId
|| (a.identifier === 'total') - (b.identifier === 'total')
);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 根据Typescript中的参数添加json对象值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50982169/