javascript - 根据Typescript中的参数添加json对象值数组

标签 javascript arrays json typescript

我有一个平面 JSON 数组,其中 identifiercategoryIdcategory 重复:

    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/

相关文章:

javascript - jquery 删除元素在 Sharepoint 内容编辑器中

javascript - Web 应用程序翻译不适用于所有浏览器

javascript - FlickrAPI 的 jsonp 问题 : Uncaught ReferenceError: jsonFlickrApi is not defined

java - 如何从包含 double 和字符串的文本文件中打印 double ?

使用 Big Huge Thesaurus API 时出现 Python 错误

json - F# JSON 解析 - 如何使用复杂路径获取属性(由多个属性名称组成)

javascript - 我如何在 `LogoHeader` 组件中判断我在哪条路线

c - 使用字符指针复制字符串

php - 基于 SQL 查询从多维数组中删除数组

python - 如何将键值对的 JSON 数组转换为 JSON 对象?