Rudziankoŭ Rudziankoŭ - 2 months ago 12
AngularJS Question

AngularJS: List comprehension in ng-repeat

I have following json structure:

$scope.info = [
{
"date": "01.10", "data": [
{'name': 'Кастусь', "behavior": "good", "attitude": "good"}
{'name': 'Андрэй', "behavior": "2 заўвагі па паводзінам", "attitude": "good"}]
},
{
"date": "24.09", "data": [
{'name': 'Кастусь', "behavior": "good", "attitude": "good"},
{'name': 'Андрэй', "behavior": "3 заўвагі па паводзінам", "attitude": " good"}]
}
//...


I would like to iterate over all names. I tried python like
ng-repeat="name in record for record in info"
but it doesn't seem accurate. Could you assist, please?

Answer

I suggest you map this to a flattened array that will give you easier sorting and filtering

$scope.info = data.reduce(function(a, c, i){
    return a.concat(c.data.map(function(item){
           item.date = c.date;
           return item
     });
},[])

Would return

$scope.info = [

   {'name': 'Кастусь', "behavior": "good", "attitude": "good", "date": "01.10"},
   {'name': 'Андрэй', "behavior": "2 заўвагі па паводзінам", "attitude": "good", "date": "01.10"}
   {'name': 'Кастусь', "behavior": "good", "attitude": "good", "date": "24.09"},
   {'name': 'Андрэй', "behavior": "3 заўвагі па паводзінам", "attitude": " good", "date": "24.09"}
]