如何使用下划线 return 对象作为 groupby 函数中的键

how to return object as key in groupby function using underscore

我的json看起来像

我正在尝试使用下划线库按 NodeGroup 分组

vm.populatedNodeGroups = _($scope.nodes).groupBy(function (o) { 
                return o.NodeGroup.Name;
            });

vm.populatedNodeGroups 中,我得到两个密钥(一个路由器,两个交换机),每个密钥都有两个数组。

我想要得到的是两个节点组对象,每个对象有两个数组。

有什么建议吗?

a working example

我会说,你快到了。这可能是我们的观点,使用下划线 groupBy:

<div ng-controller="TheCtrl">

  <div ng-repeat="(key, values) in populatedNodeGroups">
    <h3>{{key}}</h3>
    <ul>
       <li ng-repeat="value in values">{{value | json}}</li>
    </ul>
  </div>
</div>

这是我们的控制器,正在执行 groupBy

app.controller('TheCtrl', ['$scope', function ($scope) {       

      var result = _(data)
      .groupBy(function (o) { 
        return o.NodeGroup.Name;
      })
      ;

    $scope.populatedNodeGroups = result;

}])

结果会像

区域二开关

  • { "NodeGroup": { "Name": "Area Two Switches" ... } ... }
  • { "NodeGroup": { "Name": "Area Two Switches" ... } ... }

一区路由器

  • { "NodeGroup": { "Name": "Areaa One Routers" ... } ... }
  • { "NodeGroup": { "Name": "Areaa One Routers" ... } ... }

实际查看 here