Split multiple arrays into name based variables

Thinking of a better way to do this - I have arrays available:

var model1 = ['10', '20', '30', '40','50','60']; var model2 = ['80', '100', '200', '300','400','500']; var model3 = ['1', '2', '3', '4','5','6']; 

and in my code where I use them:

  $scope.sli['model1'][0]=0; $scope.sli['model1'][1]=10; $scope.sli['model1'][2]=20; $scope.sli['model1'][3]=30; $scope.sli['model1'][4]=40; $scope.sli['model1'][5]=50; $scope.sli['model1'][6]=60; 

for each model to announce them later.

What would be the best way to do this in a for loop, so I just pass the model array name, divide the array by index, so if new models are added, they will actually be automatically raised instead of being declared individually?

+5
source share
1 answer

You do not need to assign an array

if model1 defined as:

 var model1 = ['10', '20', '30', '40','50','60']; 

you can just do

 $scope.sli['model1'] = model1 

and access to individual elements eg $scope.sli['model1'][0] to get "10"

+7
source

All Articles