根据名称将多个数组拆分为变量

时间:2022-10-23 12:18:14

Thinking of a better way of doing this - I have these 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 I do:

在我使用它们的代码中我做:

    $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 declare them to use later.

为每个模型声明它们以后使用。

What would be a better way to do in a for loop, so I simply pass the model array name, split the array into an index, so if new models are added, they are actually automatically picked up, rather than declaring them individually?

在for循环中做什么是更好的方法,所以我只是传递模型数组名称,将数组拆分成索引,所以如果添加新模型,它们实际上是自动拾取的,而不是单独声明它们?

1 个解决方案

#1


7  

You dont need to assign an array

你不需要分配一个数组

if model1 has been defined as:

如果model1被定义为:

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

you can simply do

你可以干脆做

$scope.sli['model1'] = model1

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

并访问$ scope.sli ['model1'] [0]等单个元素以获得“10”

#1


7  

You dont need to assign an array

你不需要分配一个数组

if model1 has been defined as:

如果model1被定义为:

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

you can simply do

你可以干脆做

$scope.sli['model1'] = model1

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

并访问$ scope.sli ['model1'] [0]等单个元素以获得“10”