Javascript 循环遍历多个数组并保持每个元素的计数

Javascript 循环遍历多个数组并保持每个元素的计数,javascript,jquery,kendo-ui,Javascript,Jquery,Kendo Ui,我的JSON如下所示: [{ "Name": "Test Post", "Id": 123, "ProductHandlingTypes": [{ "Id": 1, "Name": "Type 1" }, { "Id": 2, "Name": "Type 2" }] }, { "Name": "Test Post 2", "Id": 124,

我的
JSON
如下所示:

[{
    "Name": "Test Post",
    "Id": 123,
    "ProductHandlingTypes": [{
         "Id": 1,
         "Name": "Type 1"
     },  
     {
         "Id": 2,
         "Name": "Type 2"
    }]
}, 
{
    "Name": "Test Post 2",
    "Id": 124,
    "ProductHandlingTypes": [{
        "Id": 3,
        "Name": "Type 3"
    },     
    {
        "Id": 1,
        "Name": "Type 1"
    }]
}]
因此,从本质上说,我希望能够循环浏览该帖子的所有产品处理类型,并将它们与产品处理类型列表进行比较。任何时候只要有匹配,我都希望跟踪特定类型被匹配/使用/找到的次数

我目前正在以这种方式检查匹配项,但我不确定如何记录每种产品类型:

postDataSource: new kendo.data.DataSource({
    transport: {
        read: { 
            url: "/path/to/get/posts",
            dataType: "json"
        }
    },
    schema: {
        parse: function (response) {
            // Get all product handling types
            viewModel.productHandlingTypesDataSource.fetch(function() {
                var types = this.data();
                // Loop through all of the posts
                for (var i = 0; i < response.length; i++) {
                    // Loop through each product handling type for each post
                    for (var j = 0; j < response[i].ProductHandlingTypes.length; j++) {
                        // Loop through every product handling type
                        for (var k = 0; k < types.length; k++) {
                            // Check if product handling type matches the post's product handling type(s)
                            if (response[i].ProductHandlingTypes[j].Name == types[k].Name) {
                                // Keep track of how many times this name matches
                            }
                        }
                    }
                }
            })

            return response;
        }
    }
})
postDataSource:new kendo.data.DataSource({
运输:{
读:{
url:“/path/to/get/posts”,
数据类型:“json”
}
},
模式:{
解析:函数(响应){
//获取所有产品处理类型
viewModel.productHandlingTypesDataSource.fetch(函数(){
var types=this.data();
//遍历所有的帖子
对于(变量i=0;i

我在试图用语言表达这个问题时遇到了一些困难,因此如果我需要进一步澄清,请让我知道。

您可以像这样使用
reduce

[{
    "Name": "Test Post",
    "Id": 123,
    "ProductHandlingTypes": [{
         "Id": 1,
         "Name": "Type 1"
     },  
     {
         "Id": 2,
         "Name": "Type 2"
    }]
}, 
{
    "Name": "Test Post 2",
    "Id": 124,
    "ProductHandlingTypes": [{
        "Id": 3,
        "Name": "Type 3"
    },     
    {
        "Id": 1,
        "Name": "Type 1"
    }]
}]
var a=[{“Name”:“TestPost”,“Id”:123,“ProductHandlingTypes”:[{“Id”:1,“Name”:“Type1”},{“Id”:2,“Name”:“Type2”},{“Name”:“TestPost2”,“Id”:124,“ProductHandlingTypes”:[{“Id”:3,“Name”:“Type3”},{“Id”:1,“Name”:“Type1”}];
变量b=a.减少((acc,cur)=>{
cur.ProductHandlingTypes.map(({Name})=>Name.forEach(n=>acc[n]=(acc[n]|0)+1);
返回acc;
}, {})

console.log(b)
您可以这样使用
reduce

[{
    "Name": "Test Post",
    "Id": 123,
    "ProductHandlingTypes": [{
         "Id": 1,
         "Name": "Type 1"
     },  
     {
         "Id": 2,
         "Name": "Type 2"
    }]
}, 
{
    "Name": "Test Post 2",
    "Id": 124,
    "ProductHandlingTypes": [{
        "Id": 3,
        "Name": "Type 3"
    },     
    {
        "Id": 1,
        "Name": "Type 1"
    }]
}]
var a=[{“Name”:“TestPost”,“Id”:123,“ProductHandlingTypes”:[{“Id”:1,“Name”:“Type1”},{“Id”:2,“Name”:“Type2”},{“Name”:“TestPost2”,“Id”:124,“ProductHandlingTypes”:[{“Id”:3,“Name”:“Type3”},{“Id”:1,“Name”:“Type1”}];
变量b=a.减少((acc,cur)=>{
cur.ProductHandlingTypes.map(({Name})=>Name.forEach(n=>acc[n]=(acc[n]|0)+1);
返回acc;
}, {})

console.log(b)
您希望输出的外观如何?您是否想要像这样的对象,以产品类型ID作为键,以计数作为值?您希望输出的外观是否可能重复?你想要像这样的东西吗?比如一个以产品类型ID为键,以计数为值的对象?可能的重复正是我想要的。谢谢这正是我想要的。谢谢