我有一个由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个具有类别和子类别属性的对象组成的数组,我想创建一个新数组,其中包含每个类别的名称和该类别的子类别数量(计数)?
阵列示例:
[{
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;
}, {});
然后使用map
将categories
转换为所需对象:
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 },
}
}
}
并且在必要时支持附加级别的嵌套。
相关文章:
- 如何用Typescript用自定义方法实现类数组
- 如何从字符串数组中获取子字符串数组
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 子级别数组上的角度计数项目
- 在Mongoose中更新子域数组而不获取父数组
- 如何制作一个CSS类数组
- 查看字符串是否包含子字符串数组中的任何元素的最佳方法?如果匹配,则进行更换
- 为什么子类数组的JSON字符串化是一个对象
- 按频率排序子字符串数组-JS
- 将参数添加到'自变量'类数组对象,性能测试
- 将Javascript类数组传递给MVC控制器
- 将数组转换为类数组对象
- 根据字符串测试子字符串数组
- JavaScript中的自定义类数组getter
- 更新$.ajax之后的类数组
- 在ES6 / ES2015中容易实例化子类数组
- 如何将类数组字符串转换为数组
- 无法查询从Mongodb返回的类数组对象
- Javascript中的类数组对象
- 如何将类数组应用于classList.contains