Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array like-:

aw_score_list = {
    '6':99,'5.5':98,'5':93,'4.5':80,'4':56,'3.5':38,'3':15,'2.5':7,'2':2,'1.5':1,'1':1,
};  

I want to convert this to html table so it will become like

keys     Values
   6         99
 5.5         98 

... and so on

please advise me how to set a for loop for it

share|improve this question
    
use ng-repeat . ng-repeat="(key, val) in aw_score_list –  RIYAJ KHAN 20 hours ago

2 Answers 2

up vote 3 down vote accepted

See ngRepeat - Iterating over object properties.

Assuming your array is in scope for the template...

<table>
<thead>
<tr>
    <th>keys</th>
    <th>Values</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, val) in aw_score_list">
    <td>{{key}}</td>
    <td>{{val}}</td>
</tr>
</tbody>
</table>
share|improve this answer
    
it is not working . let me tell you i using your code in my app like <div ng-app="gre_angu_app" class="form-group" ng-controller="gre_angu_ctrl"> <!-- angular table code-->.. </div> –  vipul sharma 20 hours ago
1  
@vipulsharma as I mentioned in my answer, I assumed you'd have something like $scope.aw_score_list = {...} in your controller –  Phil 20 hours ago
    
It is working. Please check plnkr.co/edit/OwQeFeNY68rbxX5Ug3xq?p=preview –  User2 20 hours ago
    
this is what i missed . it is working now ;) –  vipul sharma 20 hours ago
    
oh how can i get them in desc order ?? :( –  vipul sharma 19 hours ago

That is possible, but the order will be messed up, if you want to preserve order, you need something like this:

aw_score_list_preserve_order = [    
    {key:'6'   , value:99},
    {key:'5.5' , value:98},
    {key:'5'   , value:93},
    {key:'4.5' , value:80},
    {key:'4'   , value:56},
    {key:'3.5' , value:38},
    {key:'3'   , value:15},
    {key:'2.5' , value:7},
    {key:'2'   , value:2},
    {key:'1.5' , value:1},
    {key:'1'   , value:1},
  ]

this is pretty basic ng-repeat iteration, you should probably check out the angular documentation.

Plunker

share|improve this answer
    
If u want it descending ordered, u can simply do: <div ng-repeat="item in aw_score_list_preserve_order | orderBy:key:true">...</div>, if you want ascending order, take out the :true part, check documentation for orderBy. –  rabbit.aaron 19 hours ago
    
Check the plunker again, I have renew my code. You probably need your keys to be number not string to get the result you want. –  rabbit.aaron 19 hours ago
    
i can't change array sturcture .. i m following the @phil way . which is working too the only issue remain for set order by desc w.r.t. keys –  vipul sharma 19 hours ago
    
You can change the array structure in your code, like so plunker –  rabbit.aaron 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.