通过多个属性对js对象进行分组 [英] Group js objects by multiple properties

查看:53
本文介绍了通过多个属性对js对象进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我有一个对象数组,该对象按其属性的2个分组(基于 https://stackoverflow.com/a/40142591 ).

  var arr = [{"id":1"tags":"main","yearCode":"2018"},{"id":2"tags":["main","blue"],"yearCode":"2018"},{"id":3,标签":[主要",绿色"],"yearCode":"2018"},{"id":25,标签":[绿色"],"yearCode":"2018"},{"id":26,标签":[重要"],"yearCode":"2017"},{"id":29,标签":[重要",蓝色"],"yearCode":"2017"},{"id":2标签":[重要",绿色"],"yearCode":"2017"}];var mainFilter ="yearCode";var secFilter =标签";var result = arr.reduce(function(map,obj){var f1 = map [obj [mainFilter]] = map [obj [mainFilter]] ||{};var f2 = f1 [obj [secFilter]] = f1 [obj [secFilter]] ||[];f2.elements.push(obj);返回地图;},Object.create(null));console.log(JSON.stringify(result));//现在{"2017":{重要的": [{"id":26,标签":[重要"],"yearCode":"2017"}],重要的蓝色":[{"id":29,标签":[重要",蓝色"],"yearCode":"2017"}],重要的绿色":[{"id":2标签":[重要",绿色"],"yearCode":"2017"}]},"2018":{主要的": [{"id":1"tags":["main"],"yearCode":"2018"}],主蓝":[{"id":2"tags":["main","blue"],"yearCode":"2018"}],主要,绿色":[{"id":3,"tags":["main","green"],"yearCode":"2018"}],绿色": [{"id":25,标签":[绿色"],"yearCode":"2018"}]}} 

但是,我希望结果采用以下格式:

  {"2017":{重要的": [{"id":26,标签":[重要"],"yearCode":"2017"},{"id":29,标签":[重要",蓝色"],"yearCode":"2017"},{"id":2标签":[重要",绿色"],"yearCode":"2017"}],蓝色的": [{"id":29,标签":[重要",蓝色"],"yearCode":"2017"}],绿色": [{"id":2标签":[重要",绿色"],"yearCode":"2017"}]},"2018":{主要的": [{"id":1"tags":["main"],"yearCode":"2018"},{"id":2"tags":["main","blue"],"yearCode":"2018"},{"id":3,"tags":["main","green"],"yearCode":"2018"}],蓝色的": [{"id":2"tags":["main","blue"],"yearCode":"2018"}],绿色": [{"id":3,"tags":["main","green"],"yearCode":"2018"},{"id":25,标签":[绿色"],"yearCode":"2018"}]}} 

以上示例中的组仅包含数组中的一个元素,并且如果需要,每个元素都可以重复.

如果"mainFilter"也是一个数组,我想发生同样的事情(例如:如果yearCode为["2018","2017"],它将创建另一个组,就像它位于"secFilter"中一样)

通过添加一个循环来拆分值来解决

解决方案

这是您要寻找的吗?

  var arr = [{"id":1"tags":"main","yearCode":"2018"},{"id":2"tags":["main","blue"],"yearCode":"2018"},{"id":3,标签":[主要",绿色"],"yearCode":"2018"},{"id":25,标签":[绿色"],"yearCode":"2018"},{"id":26,标签":[重要"],"yearCode":"2017"},{"id":29,标签":[重要",蓝色"],"yearCode":"2017"},{"id":2标签":[重要",绿色"],"yearCode":"2017"}];var mainFilter ="yearCode";var secFilter =标签";var result = arr.reduce(function(map,obj){var f1 = map [obj [mainFilter]] = map [obj [mainFilter]] ||{};if(Object.prototype.toString.call(obj [secFilter])==='[对象数组]'){var idx;for(obj [secFilter]中的idx){var f2 = f1 [obj [secFilter] [idx]] = f1 [obj [secFilter] [idx]] ||[];f2.push(obj);}}别的{var f2 = f1 [obj [secFilter]] = f1 [obj [secFilter]] ||[];f2.push(obj);}返回地图;},Object.create(null));console.log(JSON.stringify(result));  

In the example below I have an array of objects which I group by 2 of their properties (based on https://stackoverflow.com/a/40142591).

        var arr = [{
            "id": 1,
            "tags": "main",
            "yearCode": "2018"
        }, {
            "id": 2,
            "tags": ["main", "blue"],
            "yearCode": "2018"
        }, {
            "id": 3,
            "tags": ["main", "green"],
            "yearCode": "2018"
        }, {
            "id": 25,
            "tags": ["green"],
            "yearCode": "2018"
        }, {
            "id": 26,
            "tags": ["important"],
            "yearCode": "2017"
        }, {
            "id": 29,
            "tags": ["important", "blue"],
            "yearCode": "2017"
        }, {
            "id": 2,
            "tags": ["important", "green"],
            "yearCode": "2017"
        }];


        var mainFilter = "yearCode";
        var secFilter = "tags";
        var result = arr.reduce(function(map, obj) {

          var f1 = map[obj[mainFilter]] = map[obj[mainFilter]] || {};

          var f2 = f1[obj[secFilter]] = f1[obj[secFilter]] || [];

          f2.elements.push(obj);

          return map;
        }, Object.create(null));

        console.log(JSON.stringify(result));



        // NOW
        {
          "2017": {
            "important": [
              {
                "id": 26,
                "tags": ["important"],
                "yearCode": "2017"
              }
            ],
            "important,blue": [
              {
                "id": 29,
                "tags": ["important","blue"],
                "yearCode": "2017"
              }
            ],
            "important,green": [
              {
                "id": 2,
                "tags": ["important","green"],
                "yearCode": "2017"
              }
            ]
          },
          "2018": {
            "main": [
              {
                "id": 1,
                "tags": ["main"],
                "yearCode": "2018"
              }
            ],
            "main,blue": [
              {
                "id": 2,
                "tags": ["main","blue"],
                "yearCode": "2018"
              }
            ],
            "main,green": [
              {
                "id": 3,
                "tags": ["main","green"],
                "yearCode": "2018"
              }
            ],
            "green": [
              {
                "id": 25,
                "tags": ["green"],
                "yearCode": "2018"
              }
            ]
          }
        }

However, I would like the results to be in the following format:

                {
                  "2017": {
                    "important": [
                      {
                        "id": 26,
                        "tags": ["important"],
                        "yearCode": "2017"
                      },
                      {
                        "id": 29,
                        "tags": ["important","blue"],
                        "yearCode": "2017"
                      },
                      {
                        "id": 2,
                        "tags": ["important","green"],
                        "yearCode": "2017"
                      }
                    ],
                    "blue": [
                      {
                        "id": 29,
                        "tags": ["important","blue"],
                        "yearCode": "2017"
                      }
                    ],
                    "green": [
                      {
                        "id": 2,
                        "tags": ["important","green"],
                        "yearCode": "2017"
                      }
                    ]
                  },
                  "2018": {
                    "main": [
                      {
                        "id": 1,
                        "tags": ["main"],
                        "yearCode": "2018"
                      },
                      {
                        "id": 2,
                        "tags": ["main","blue"],
                        "yearCode": "2018"
                      },
                      {
                        "id": 3,
                        "tags": ["main","green"],
                        "yearCode": "2018"
                      }
                    ],
                    "blue": [
                      {
                        "id": 2,
                        "tags": ["main","blue"],
                        "yearCode": "2018"
                      }
                    ],
                    "green": [
                      {
                        "id": 3,
                        "tags": ["main","green"],
                        "yearCode": "2018"
                      },
                       {
                        "id": 25,
                        "tags": ["green"],
                        "yearCode": "2018"
                      }
                    ]
                  }
                }

The groups in the example above include only one of the elements in the array, and each element may be duplicated if needed.

EDIT: I would like the same to happen if "mainFilter" is also an array (for example: if yearCode is ["2018","2017"], it will create another group just like if it would be in "secFilter")

EDIT 2: solved by adding a loop to split the values

解决方案

Is this what you are looking for ?

var arr = [{
            "id": 1,
            "tags": "main",
            "yearCode": "2018"
        }, {
            "id": 2,
            "tags": ["main", "blue"],
            "yearCode": "2018"
        }, {
            "id": 3,
            "tags": ["main", "green"],
            "yearCode": "2018"
        }, {
            "id": 25,
            "tags": ["green"],
            "yearCode": "2018"
        }, {
            "id": 26,
            "tags": ["important"],
            "yearCode": "2017"
        }, {
            "id": 29,
            "tags": ["important", "blue"],
            "yearCode": "2017"
        }, {
            "id": 2,
            "tags": ["important", "green"],
            "yearCode": "2017"
        }];


        var mainFilter = "yearCode";
        var secFilter = "tags";
        var result = arr.reduce(function(map, obj) 
        {

          var f1 = map[obj[mainFilter]] = map[obj[mainFilter]] || {};

          if(Object.prototype.toString.call(obj[secFilter]) === '[object Array]')
          {
            var idx;
            for(idx in obj[secFilter])
            {
              var f2 = f1[obj[secFilter][idx]] = f1[obj[secFilter][idx]] || [];
              f2.push(obj);             
            }
          }
          else
          {
            var f2 = f1[obj[secFilter]] = f1[obj[secFilter]] || [];
            f2.push(obj);
          }

          return map;
        }, Object.create(null));

        console.log(JSON.stringify(result));

这篇关于通过多个属性对js对象进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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