0

I'm working on this codepen. Here is the link. http://codepen.io/sweenj7/pen/RPYrrE

I'm having trouble in this paticular area.

 //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] );

I'm trying to get each value of the array to print in the accordion list in it's indexed spot. For some reason I am getting a TypeError that 0 is undefied, but when I have it print the entire array with weightReps instead of weightReps[i or 1,2 etc] it works and prints the entire array. Not sure what the solution is for this.

  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;
  };

});
3
  • 1
    you don't have the weightReps property assigned on all of your objects; when it hits excercises[1], there is no weightReps property, so .weightReps[0] would be undefined. Commented Jul 21, 2015 at 17:24
  • Suggestion...seems like working with mapping arrays and objects is new...try working with them in a simple sandbox that just displays your data and allows you to quickly try things to manipulate that data Example: plnkr.co/edit/vxgyGS12yx4PgOqWw34p Commented Jul 21, 2015 at 17:37
  • There are defintiely a number of tools you could use to simplify what you are doing like Array.prototype.map() Commented Jul 21, 2015 at 17:39

1 Answer 1

1

Curls and Squats do not have the property weightReps so it bombs when you try to read said property.

Modify the collection to this to add the property, but you also need to add reps to each weightReps collection so that when you call [object].weightReps[0] you don't get back a null.

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]};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone, Adding the weightReps and eightSets to the other objects fixed the issue. I thought it would work for the first one and bomb out further down the loop. This works perfect. code $scope.groups[i].items.push("Set " + j + " " + exercises[i].weightSets[j-1] );code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.