我想动态创建一个编号对象的数组 $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