【问题标题】:split multiple arrays into variables based on name根据名称将多个数组拆分为变量
【发布时间】:2015-09-22 02:11:22
【问题描述】:

正在考虑一种更好的方法 - 我有这些可用的数组:

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

在我使用它们的代码中:

    $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 循环中更好的方法是什么,所以我只需传递模型数组名称,将数组拆分为索引,因此如果添加了新模型,它们实际上会被自动拾取,而不是声明它们个人?

【问题讨论】:

    标签: javascript arrays angularjs multidimensional-array


    【解决方案1】:

    你不需要分配一个数组

    如果model1 已定义为:

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

    你可以这样做

    $scope.sli['model1'] = model1
    

    并访问单个元素 点赞$scope.sli['model1'][0] 获取"10"

    【讨论】:

      猜你喜欢
      • 2014-12-29
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2021-01-27
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多