javascript - how to return object as key in groupby function using underscore -
my json
looks
i trying group nodegroup using underscore library
vm.populatednodegroups = _($scope.nodes).groupby(function (o) { return o.nodegroup.name; });
in vm.populatednodegroups
2 keys (are 1 routers , 2 switches) 2 arrays each.
what trying 2 node group objects 2 arrays each.
any suggestions?
there a working example
i say, there. our view, consuming underscore 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>
here our controller, doing groupby
app.controller('thectrl', ['$scope', function ($scope) { var result = _(data) .groupby(function (o) { return o.nodegroup.name; }) ; $scope.populatednodegroups = result; }])
the result
area 2 switches
{ "nodegroup": { "name": "area 2 switches" ... } ... }
{ "nodegroup": { "name": "area 2 switches" ... } ... }
areaa 1 routers
{ "nodegroup": { "name": "areaa 1 routers" ... } ... }
{ "nodegroup": { "name": "areaa 1 routers" ... } ... }
check in action here
Comments
Post a Comment