计算Javascript对象数组中的重复对象 [英] Counting duplicate objects in array of Javascript objects

查看:102
本文介绍了计算Javascript对象数组中的重复对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析Javascript对象的子对象以计算相同的子对象的数量,并返回具有这些计数的新JSON对象。这可能不是很清楚,所以这里是小提琴

I'm trying to parse the sub-objects of a Javascript object to count the number of sub-objects that are the same and return a new JSON object with those counts. That's probably not very clear, so here's the fiddle.

在小提琴中给出 crsListData ,我想返回这个:

Given the crsListData in the fiddle, I'd like to return this:

objToReturn = [
            {
                key: "Consigned",
                values:
                    [
                        { mapKey: 2007 (4), mapVal: [["Ford F-150 (3)"], ["Honda Civic (1)"]] },
                        { mapKey: 2011 (1), mapVal: "Toyota Camry (1)" },
                        { mapKey: 2005 (1), mapVal: "Dodge RAM (1)" }
                    ]
            },
            {
                key: "Run",
                values:
                    [
                        { mapKey: 2007 (2), mapVal: "Ford F-150 (2)" },
                        { mapKey: 2011 (1), mapVal: "Toyota Camry (1)" }
                    ]
            },
            {
                key: "Sold",
                values:
                    [
                        { mapKey: 2007 (1), mapVal: "Ford F-150 (1)" },
                        { mapKey: 2011 (1), mapVal: "Toyota Camry (1)" }
                    ]
            }
        ];

如果您在小提琴中注意到,我评论了关于mapKey和mapValue的部分内容。它们将由从选择选项获得的函数参数确定,按模型(计数)/年份(计数)或年份(计数)/模型(计数)排序。

If you'll notice in the fiddle, I've commented on a part about the mapKey and mapValue. They will be determined by function parameters obtained from a select option to order by Model (count)/Year (count), or Year (count)/Model (count).

我的最终目标是生成一个基本上如下所示的HTML树:

My end goal here is to generate an HTML tree that essentially looks like this:


  1. 寄售(寄售#)

  1. Consigned (# of consigned)


  • 2007(2007款车型总数)

    • 型号1(计数)

    • 模型2(计数)

    运行(运行次数)


    • 2007(总计数2007款)

      • 型号1(计数)

      • 型号2(计数)

      年份的顺序应基于该年度的模型总数(最高#第一)。同样,这些年份下的模型的顺序应按最高计数排序。

      The order of the years should be based on the total count of models for that year (highest # first). Similarly, the order of the models under the years should be ordered by highest count.

      或者,如果用户选择模型/年份进行分组,我想生成:

      Alternatively, if the user selects Model/Year for the grouping, I'd like to generate:


      1. 托运(寄出#)

      1. Consigned (# of consigned)


      • Model1(所有年份此型号的总数)

        • 第1年(今年的模特数)

        • 2年级(今年的模特数)

        运行(运行次数)


        • Model1 (所有年份此型号的总数)

          • 第1年(今年的型号数量)

          • 第2年(计数)今年的模型)

          此顺序将基于计数同样。如果您需要任何澄清,请告诉我!谢谢!

          The ordering of this would be based on counts as well. Let me know if you need any clarification! Thanks!

          推荐答案

          你想要做的事情的根本很简单。您想要按某些属性对数据进行分组。让它看起来很复杂的是你想要在几个级别上做。

          The root of what you want to do is simple. You want to group data by some property. What makes it seem complicated is that you want to do it at several levels.

          首先你有3组数据寄售,运行,已售出。解决它为一个,你解决所有。

          First you have 3 set of data "Consigned", "Run", "Sold". Solve it for one and you solve it for all.

          首先你按车型分组汽车。在模型中,您可以按年份分组。同样,它的核心是你想要通过某些属性对数据进行分组。

          First you have the cars grouped by model. Within the model you have the cars grouped by year. Again the core of it though is that you want to group data by some property.

          你可以使用这样的函数来做到这一点:

          You can use a function like this to do that:

          var cars = [
                      { year: 2007, model: "Ford F-150" },
                      { year: 2011, model: "Toyota Camry" },
                      { year: 2007, model: "Ford F-150" },
                      { year: 2007, model: "Ford F-150" },
                      { year: 2005, model: "Dodge RAM" }
                  ];
          
          function groupBy(propertyName, array) {
              var groupedElements = {};
          
              for(var i = 0; i < array.length; i++) {
                  var element = array[i];
                  var value = element[propertyName];
          
                  var group = groupedElements[value];
                  if(group == undefined) {
                      group = [element];
                      groupedElements[value] = group;
                  } else {
                      group.push(element);
                  }
              }
          
              return groupedElements;
          }
          
          var result = groupBy("year", cars)
          

          输出将是:

          {
              "2005": [
                  {
                      "year": 2005,
                      "model": "Dodge RAM"
                  }
              ],
              "2007": [
                  {
                      "year": 2007,
                      "model": "Ford F-150"
                  },
                  {
                      "year": 2007,
                      "model": "Ford F-150"
                  },
                  {
                      "year": 2007,
                      "model": "Ford F-150"
                  }
              ],
              "2011": [
                  {
                      "year": 2011,
                      "model": "Toyota Camry"
                  }
              ]
          }
          

          你可以在这个JSFiddle中使用它: http://jsfiddle.net/luisperezphd/jCt2k/

          You can play with it in this JSFiddle: http://jsfiddle.net/luisperezphd/jCt2k/

          你可以完成你想要的做这个分组a每个级别。第一组逐年。然后逐个浏览每个生成组。您的计数将是值数组的长度

          You can accomplish what you want to doing this grouping at each level. First group by year. Then go through each of the generate groups and group by year. Your count will be the length of the values array.

          这篇关于计算Javascript对象数组中的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆