【问题标题】:JavaScript group objectsJavaScript 组对象
【发布时间】:2026-01-14 14:25:01
【问题描述】:

我已经找了几天了,我还没有找到这个特定问题的答案。我从我的 API 中的端点接收一个 javascript 对象数组。我需要根据类型将对象组合在一起。

对象硬编码数组示例:

$scope.content = {
    things: [
        {
            thing: 'one',
            thingType: 'load'
        },
        {
            thing: 'two',
            thingType: 'export'
        },
        {
            thing: 'three',
            thingType: 'export'
        }
    ]
}
var typeArr = [];
for (var key in $scope.content.things) {

     typeArr[key] = $scope.content.things[key].thingType;
}
typeArr = _.uniq(typeArr);

typeArr 现在将是 [load, export] 我接下来需要做的是比较 things[ ] 中的所有对象,这样

 if(typeArr[key] === things[i].thingType) 

会像这样推动那个对象:

typeArr = [
    load: {
        thing: 'one',
        thingType: 'load'
    },
    export: [{
        thing: 'two',
        thingType: 'export'
    },
    {
        thing: 'three',
        thingType: 'export'
     }

    ]
]

换句话说,我需要对象保持完整,我需要对它们进行分类,并根据共享类型嵌套它们。我整个星期都被严重困住了。任何见解将不胜感激。

【问题讨论】:

    标签: javascript angularjs underscore.js


    【解决方案1】:

    试试这个

       
    var content = {
        things: [
            {
                thing: 'one',
                thingType: 'load'
            },
            {
                thing: 'two',
                thingType: 'export'
            },
            {
                thing: 'three',
                thingType: 'export'
            }
        ]
    }
    var typeArr = {};
    content.things.forEach(function(item){
        typeArr[item.thingType] = typeArr[item.thingType]||[];
        typeArr[item.thingType].push(item);
    });
    
    document.body.innerHTML = JSON.stringify(typeArr);

    【讨论】:

    • 这很好用。它正在适当地返回完整列表。我对 JS 很陌生,来自 C#,仍然不太习惯使用这样的匿名函数。非常感谢您的及时回复!
    【解决方案2】:

    这样的事情可以接受吗?

    var things = [
        {
            thing: 'one',
            thingType: 'load'
        },
        {
            thing: 'two',
            thingType: 'export'
        },
        {
            thing: 'three',
            thingType: 'export'
        }
    ];
    
    var types = {};
    
    for (i in things) {
      var thing = things[i];
      if (typeof types[thing.thingType] === "undefined") {
        types[thing.thingType] = [];
      }
      types[thing.thingType].push(thing);
    }
    
    console.log(types);

    生成的 types 对象看起来像

    {
        load: [{
            thing: 'one',
            thingType: 'load'
        }],
        export: [{
            thing: 'two',
            thingType: 'export'
        },
        {
            thing: 'three',
            thingType: 'export'
         }]
    }
    

    也就是说,每个类型始终是一个数组,即使它只包含一个项目(在您的示例中它只是一个对象)。但我认为无论如何这对你来说会更容易处理,因为你知道你总是期望一个数组。

    【讨论】:

      【解决方案3】:

      不是 100% 我知道你的目标是什么,但我会试一试。这是你的意思吗..

      Demo

      $scope.content = {
            things: [
                {
                    thing: 'one',
                    thingType: 'load'
                },
                {
                    thing: 'two',
                    thingType: 'export'
                },
                {
                    thing: 'three',
                    thingType: 'export'
                }
            ]
        }
      
        $scope.groups = {};
        $scope.group  = group;
      
      
        function group(){
      
          angular.forEach($scope.content.things, function(thing){
      
            if($scope.groups.hasOwnProperty(thing.thingType)){
              $scope.groups[thing.thingType].push(thing);
            } else {
              $scope.groups[thing.thingType] = [thing];
            }
      
          });
      
      
        }
      

      【讨论】: