【问题标题】:Group js objects by multiple properties通过多个属性对 js 对象进行分组
【发布时间】:2018-08-15 12:12:20
【问题描述】:

在下面的示例中,我有一个对象数组,我按它们的 2 个属性分组(基于 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"
              }
            ]
          }
        }

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

                {
                  "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"
                      }
                    ]
                  }
                }

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

编辑: 如果“mainFilter”也是一个数组,我希望发生同样的事情(例如:如果 yearCode 是 [“2018”,”2017”],它将创建另一个组,就像它在“secFilter”中一样)

编辑 2:通过添加循环来拆分值来解决

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    这是你要找的吗?

    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));

    【讨论】:

      【解决方案2】:

      我相信下面的分组应该可以解决问题:

      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((map, obj) => {
      
          if (!map[obj[mainFilter]]) map[obj[mainFilter]] = {};
      
          [].concat(obj[secFilter]).forEach(subEl => {
      
              if (!map[obj[mainFilter]][subEl]) map[obj[mainFilter]][subEl] = [];
      
              map[obj[mainFilter]][subEl].push(obj);
      
          });
      
          return map;
      
      }, {});
      
      console.log(result);

      【讨论】:

        猜你喜欢
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-13
        • 2022-01-24
        • 1970-01-01
        • 2018-02-20
        • 2018-08-28
        相关资源
        最近更新 更多