0

I'm trying to show the values of a specific JSON in the right Order. My JSON looks like :

{
"A":[{"id":"21","name":"Andrea"},{"id":"22","name":"Apple"}],
"B":[{"id":"21","name":"Baby"},{"id":"22","name":"Bali"}],
"C":[{"id":"21","name":"Candle"},{"id":"22","name":"Canada"}],
}

How to show values with ng-repeat :

<div ng-repeat="item in items">

</div>

like :

A

Andrea

Apple

B

Baby

Bali

C

Candle

Canada

Stranger B.
  • 9,004
  • 21
  • 71
  • 108
  • Similar to: http://stackoverflow.com/questions/15127834/how-can-i-iterate-over-the-keys-value-in-ng-repeat-in-angular – khakiout Jul 08 '15 at 09:13
  • Possible duplicate of [How to iterate over the keys and values with ng-repeat in AngularJS?](https://stackoverflow.com/questions/15127834/how-to-iterate-over-the-keys-and-values-with-ng-repeat-in-angularjs) – xIsra Sep 22 '19 at 12:03

4 Answers4

5

Please check working example: http://plnkr.co/edit/lqoQvmXnimWYzqVU0wkg?p=preview

HTML

<div ng-repeat="(key, values) in items">
  {{key}}
  <div ng-repeat="item in values">
    {{item.name}}
  </div>
</div>
User2
  • 1,293
  • 8
  • 17
1

you can iterate over the object with:

<div ng-repeat="(key, value) in jsonData">
   {{key}}
   <div ng-repeat="item in value">
      <div>{{item.name}}</div>
   </div>
</div>

edit: I see the other guys added jsfiddle so enjoy :)

Dima Gimburg
  • 1,376
  • 3
  • 17
  • 35
1

If your data be like this :

$scope.items={
"A":[{"id":"21","name":"Andrea"},{"id":"22","name":"Apple"}],
"B":[{"id":"21","name":"Baby"},{"id":"22","name":"Bali"}],
"C":[{"id":"21","name":"Candle"},{"id":"22","name":"Canada"}],
}

Consider item as map and iterate for (key,value) in map and the value will be associated List. And again iterate for the list in values as -

<div ng-repeat="(key,value) in items">
     {{key}}
     <div ng-repeat="item in value">
       {{item.name}}
     </div>
</div>
Anita
  • 2,352
  • 2
  • 19
  • 30
0

Try this plunker.

You print key of your json too so you should use '(key,value)' in ng-repeat to get value of key too.

code:

<div ng-repeat="(key,value) in data">
   {{key}}
    <div ng-repeat="item in value">
      {{item}}
    </div>
</div>
Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32