How do I populate angularjs list with for loop or array?

Viewed 36

I have the following code someone hard coded:

$scope.years = [
   {id:curryear, name: curryear}
   {id:curryear - 1, name: curryear - 1}
   {id:curryear - 2, name: curryear -2}
];

My task is to populate it so that the HTML dropdown it is linked to can get every year within a certain range. I am pretty new to using angularjs so I am not sure about what I can do with it. Is there a way to iterate through the range, lets say 2020 to 2010 while populating it? Or can I make an array of sets in the JS function this resides in and set $scope.years equal to that>

1 Answers

For example you want to start from 2020 and you want to go till 2010:

const years = [{id: 2020, name: 2020}];
for ( let i = 1; i <= 10; i++ ) {
    years.push({id: years[i-1].id - 1, name: years[i-1].name - 1});
 }
Related