0

我正在研究此codepen。链接在这里。 http://codepen.io/sweenj7/pen/RPYrrE从对象中的数组中获取值,范围问题

我在这个特殊区域遇到了麻烦。

//This prints the entire array 
     $scope.groups[i].items.push("Set " + j + " " +  exercises[i].weightReps); 
     //This gets the following error TypeError: Cannot read property '0' of undefined 
     //$scope.groups[i].items.push("Set " + j + " " + exercises[i].weightReps[i]); 

我试图让数组的每个值在打印在索引位置的手风琴列表中。出于某种原因,我得到一个TypeError,0是未被满足,但是当我有它打印整个数组weightReps而不是weightReps [我或1,2等]它的工作原理和打印整个数组。不知道这个解决方案是什么。

angular.module('ionicApp', ['ionic']) 

    .controller('MyCtrl', function($scope) { 
$scope.groups = []; 

    var Bench = { name:"Bench", StartingSets:"5", Instructions:"lift up arm press to chest", weightSets: ["125","200","200","245","150"], weightReps: ["8","8","10","10","15"] }; 
    var Curls = { name:"Curls", StartingSets:"3", Instructions:"lift up arm"}; 
    var Squat = { name:"Squat", StartingSets:"4", Instructions:"Squat Down"}; 
var exercises = new Array(); 
exercises[0] = Bench; 
exercises[1] = Curls; 
exercises[2] = Squat; 

for (var i=0; i < exercises.length; i++) { 
for (var key in exercises[i]) { 

$scope.groups[i] = { 
    name: exercises[i][Object.keys(exercises[i])[0]] + ' - ' + exercises[i][Object.keys(exercises[i])[1]] + " Sets" , 
    items: [] 
} 
}; 
$scope.groups[i].items.push(exercises[i].Instructions); 
for (var j=1; j-1<exercises[i][Object.keys(exercises[i])[1]]; j++) { 
    //for (var key in exercises[i]) { 
// $scope.groups[i].items.push(JSON.stringify(exercises[i]) + '-' + j); 
    //$scope.groups[i].items.push(exercises[i].StartingSets + '-' + j); 
    //console.log(exercises[i].weightReps[i]); 
    //This prints the entire array 
    $scope.groups[i].items.push("Set " + j + " " + exercises[i].weightReps); 
    //This gets the following error TypeError: Cannot read property '0' of undefined 
    //$scope.groups[i].items.push("Set " + j + " " + exercises[i].weightReps[i]); 

    //$scope.groups[i].items.push(exercises[i][key] + '-' + j); 
} 
    } 

/* 
    * if given group is the selected group, deselect it 
    * else, select the given group 
    */ 
    $scope.toggleGroup = function(group) { 
    if ($scope.isGroupShown(group)) { 
    $scope.shownGroup = null; 
} else { 
    $scope.shownGroup = group; 
} 
    }; 
$scope.isGroupShown = function(group) { 
     return $scope.shownGroup === group; 
    }; 

}); 
+1

您没有在所有对象上分配'weightReps'属性;当它碰到'excercises [1]'时,没有'weightReps'属性,所以'.weightReps [0]'是'undefined'。 – Claies

+0

Suggestion ...似乎像使用映射数组和对象是新的...尝试在简单的沙箱中使用它们来显示数据,并允许您快速尝试操作该数据。示例:http:// plnkr。编辑/ vxgyGS12yx4PgOqWw34p – charlietfl

+0

有几个工具可以用来简化你正在做的事情,比如'Array.prototype.map()' – charlietfl

回答

1

卷发和深蹲没有财产weightReps所以它的炸弹,当你试图读取上述财产。

将集合修改为此以添加属性,但还需要将代表添加到每个weightReps集合中,以便在调用[object].weightReps[0]时不返回空值。

var Bench = { name:"Bench", StartingSets:"5", Instructions:"lift up arm press to chest", weightSets: ["125","200","200","245","150"], weightReps: ["8","8","10","10","15"] }; 
var Curls = { name:"Curls", StartingSets:"3", Instructions:"lift up arm", weightSets: [ADD SOME SETS HERE]}; 
var Squat = { name:"Squat", StartingSets:"4", Instructions:"Squat Down",weightSets: [ADD SOME SETS HERE]}; 
+0

谢谢大家,将weightReps和eightSets添加到其他对象解决了问题。我认为它会为第一个工作,并进一步向下循环。这工作完美。 'code' $ scope.groups [i] .items.push(“Set”+ j +“”+ exercises [i] .weightSets [j-1]);'code' – SwimJim