Angularjs 角度初始化数组并根据另一个$scopes.length填充数字

Angularjs 角度初始化数组并根据另一个$scopes.length填充数字,angularjs,arrays,Angularjs,Arrays,我想动态创建一个编号对象数组 $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 j

我想动态创建一个编号对象数组
$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\u products.length);
$scope.quizAnswers=[];
for(var i=0;i
我不明白你在问什么。顾名思义,AngularJS使用JavaScript。我使用$scope.answers=function(empId){for(var I=0;I<$scope.myQuestions.length;I++{if($scope.myQuestions[I].id==empId){返回$scope.myQuestions[I].$index;} }; };编辑您的问题,并在问题中张贴您尝试过的内容,格式正确。清楚地解释你想要达到的目标。您的代码使用empId,检查问题的ID等,尽管您的解释目前说t应该只创建一个包含从0到myQuestions长度的数字的数组。您甚至只能对(var i=0;i<$scope.edition\u products.length;i++){$scope.quizAnswers.push({ID:i,Index:i,“Answered”):“No”,“Correct”:“null”};}``无需添加新变量来计算
length
$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