我有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个对象与类别和子类别和属性阵列需要创建一个子类别的计数的一个新的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类别和子类别的属性100对象的数组,我想创建一个每个类别的名称和子类别的该类别的数量(计数)一个新的数组?
阵列的示例:
[{
ticketId:1,
分类:驱动程序,
类别ID:29,
子类别:监视器,
SubCategoryID:31
},{
ticketId:2,
分类:驱动程序,
类别ID:29,
子类别:监视器,
SubCategoryID:31
},{
ticketId:3,
分类:硬件,
类别ID:11,
子类别:监视器,
SubCategoryID:32
},{
ticketId:4,
分类:硬件,
类别ID:11,
子类别:手机,
SubCategoryID:13
}];
类别列表举例:
[{
ID:1,
PARENTID:0,
名称:打印
},{
ID:2,
PARENTID:1,
名称:墨粉
},{
ID:3,
PARENTID:1,
名称:电源
},{
ID:4,
PARENTID:1,
名称:卡纸
},{
ID:5,
PARENTID:0,
名:Office应用程序
}]
家长0 ID是分类和任何其他号为子类别
解决方案
也许这对你的作品。它利用 Array.prototype 。降低()
\r
\r\r
\r VAR数据= {[ticketId:1,类别:驱动程序,类别ID: 29,子类别:监视器,SubCategoryID:31},{ticketId:2,类别:驱动程序,类别ID:29次类别:监视器,SubCategoryID:31},{ticketId:3,类别:硬件,类别ID:11次类别:监视器,SubCategoryID:32},{ticketId:4,类别:硬件,类别ID:11次类别:手机,SubCategoryID:13}],\r
数= data.reduce(函数(R,A){\r
- [R [a.Category] = R [a.Category] || {};\r
ř[a.Category] [a.SubCategory] =(R [a.Category] [a.SubCategory] || 0)+ 1;\r
返回ř;\r
},{});\r
\r
的document.write('< pre>'+ JSON.stringify(计数,0,4)+'< / pre>');
\r
单单只出现在类别
计数
\r
\r\r
\r VAR数据= {[ticketId:1,类别:驱动程序,类别ID: 29,子类别:监视器,SubCategoryID:31},{ticketId:2,类别:驱动程序,类别ID:29次类别:监视器,SubCategoryID:31},{ticketId:3,类别:硬件,类别ID:11次类别:监视器,SubCategoryID:32},{ticketId:4,类别:硬件,类别ID:11次类别:手机,SubCategoryID:13}],\r
数= data.reduce(函数(R,A){\r
ř[a.Category] =(R [a.Category] || 0)+ 1;\r
返回ř;\r
},{});\r
\r
的document.write('< pre>'+ JSON.stringify(计数,0,4)+'< / pre>');
\r
I have an array of 100 objects with category and subcategory properties and I want to create a new array with the name of each category and the number(count) of subcategories for that category?
Example of array:
[{
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
}];
Example of list of categories:
[{
"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"
}]
Parent ID of 0 being the Category and any other number being the SubCategory
解决方案
Maybe this works for you. It utilizes 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>');
Just only the counts in 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>');
这篇关于我有100个对象与类别和子类别和属性阵列需要创建一个子类别的计数的一个新的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文