我有一个由100个具有类别和子类别属性的对象组成的数组,需要创建一个新的子类别计数数组

I have an array of 100 objects with properties of category and subcategory and need to create a new array of the count of subcategories?

本文关键字:子类 数组 数数 创建 一个 100个 有一个 对象 属性      更新时间:2023-09-26

我有一个由100个具有类别和子类别属性的对象组成的数组,我想创建一个新数组,其中包含每个类别的名称和该类别的子类别数量(计数)?

阵列示例:

[{
    ticketId: 1,
    Category: "Driver",
    CategoryID: 29,
    SubCategory: "Monitor",
    SubCategoryID: 31
}, {
    ticketId: 2,
    Category: "Driver",
    CategoryID: 29,
    SubCategory: "Monitor",
    SubCategoryID: 31
}, {
    ticketId: 3,
    Category: "Hardware",
    CategoryID: 11,
    SubCategory: "Monitor",
    SubCategoryID: 32
}, {
    ticketId: 4,
    Category: "Hardware",
    CategoryID: 11,
    SubCategory: "phone",
    SubCategoryID: 13
}];

类别列表示例:

[{
    "ID": 1,
    "ParentID": 0,
    "Name": "Printing"
}, {
    "ID": 2,
    "ParentID": 1,
    "Name": "Toner"
}, {
    "ID": 3,
    "ParentID": 1,
    "Name": "Power"
}, {
    "ID": 4,
    "ParentID": 1,
    "Name": "Paper Jam"
}, {
    "ID": 5,
    "ParentID": 0,
    "Name": "Office Applications"
}]

0的父ID是类别,任何其他数字是子类别

也许这对你有用。它使用Array.prototype.reduce()

var data = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 }],
    count = data.reduce(function (r, a) {
        r[a.Category] = r[a.Category] || {};
        r[a.Category][a.SubCategory] = (r[a.Category][a.SubCategory] || 0) + 1;
        return r;
    }, {});
document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');

Category:中的计数

var data = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 }],
    count = data.reduce(function (r, a) {
        r[a.Category] = (r[a.Category] || 0) + 1;
        return r;
    }, {});
document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');

使用reduce:首次计数每个类别的子类别数

var subcategoriesCounts = subcats.reduce(function(acc, cur) {
    acc[cur.CategoryID] = (acc[cur.CategoryID] || 0) + 1;
    return acc;
}, {});

然后使用mapcategories转换为所需对象:

var result = categories.map(function(cat) {
    return {
        name: cat.Name,
        count: subcategoriesCounts[cat.ID] || 0
    }
});

var arr = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 } ];
var map = {}
for(var i = 0; i < arr.length; i++) {
  console.log(arr[i])
    if (!map[arr[i].Category]) {
      map[arr[i].Category] = {
        count: 1,
        parentId: arr[i].CategoryID,
        categoryId: arr[i].SubCategoryID,
        categoryName: arr[i].SubCategory
      };
    } else {
      map[arr[i].Category].count++;
    }
}
var results = [];
for (var category in map) {
  results.push(map[category]);
}
var el = document.getElementById("result").innerHTML = JSON.stringify(results, null, '  ')
<pre id="result"></pre>

//FORMAT

var list = [
    {
        "ticketID": 1, 
        "category": "driver", 
        "subcategories": ['toner','power','paper jam','office applications']
    },
    {
        "ticketID": 2, 
        "category": "driver", 
        "subcategories": ['toner','power']
    },
];

然后你可以简单地通过console.log(list[0].subcategories.length)

以及访问它们console.log(list[0].subcategories[0])

要在同一结构中组合两个级别的计数,必须将计数与子级分开。如果您既将顶级计数存储为属性,又将子项本身存储,则会导致混乱。

这样可以产生更好的结构:

var counts = data.reduce(function(r, o) {
    var cat = o.Category;
    var sub = o.SubCategory;
    r[cat] = r[cat] || { count: 0, children: {} }
    r[cat].count++;
    r[cat].children[sub] = r[cat].children[sub] || { count: 0 }
    r[cat].children[sub].count++;
    return r;
}, {});

您的数据将产生:

{
  "Driver": {
    count: 2,
    children: {
        "Monitor": { count: 2 }
    },
  },
  "Hardware": {
    count: 2,
    children: {
      "Monitor": { count: 1},
      "phone": { count: 1 },
    }
  }
}

并且在必要时支持附加级别的嵌套。