2

I have a an array of objects like this in angular:

$scope.data = [
    {name:"John", group:"a"},
    {name:"David", group:"a"},
    {name:"Tom", group:"b"},
];

I want to present this data as something like this (in a template):

<h2>group a</h2>
John<br/>
David<br/>

<h2>groub b</h2>
Tom<br>

How would you suggest to do this? How can I go from the structure at the beginning to the structure at the end?

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
jjei
  • 1,180
  • 3
  • 13
  • 21

1 Answers1

5

You could use grouby filter that would do group for you on basis of group property

Markup

<div ng-repeat="(key, value) in data | groupBy: 'group'">
  <h1>group {{ key }}</h1> 
  <div ng-repeat="person in value">
    {{ person.name }} 
  </div>
</div>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Be sure to install https://github.com/a8m/angular-filter because groupBy requires it. – jjei Aug 25 '15 at 12:47