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