Angular 初始化数组并填充基于另一个 $scopes.length 的数字



我想动态创建一个编号对象的数组 $scope.answers [];基于另一个$scope的长度,例如 $scope.myQuestions.length答案的编号

因此,如果$scope.myQuestions.length为 5,则结果将是

     [ 0, 1, 2, 3, 4 ]
       Ref:  http://2ality.com/2013/11/initializing-arrays.html
      This is how it's done in Javascript just need it juxtaposing into     Angular
     Array(undefined, undefined, undefined)
    Array.apply(null, Array(3))
   If we combine this trick with map(), we get what we wanted:
       function fillArrayWithNumbers(n) {
    var arr = Array.apply(null, Array(n));
    return arr.map(function (x, i) { return i });
   }
   The function in action:
      > fillArrayWithNumbers(5)
         [ 0, 1, 2, 3, 4 ]
$scope.length = parseInt($scope.edition_products.length);
            $scope.quizAnswers = [];
                   for(var i = 0 ; i < parseInt($scope.length) ; i++)
                    $scope.quizAnswers.push({id:i, Index:i,  "Answered":"No","Correct":"null"});   worked for me  

最新更新