Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Object Manipulation Questions

At the moment I am working on an ionic app (angular1) and I am working on a huge object.

The Object will look like this:

userWorkouts: [
  {
    title: '3 Split',
    id: 1,
    workoutImg: '',
    workoutSessions: {
      1: {
        workoutSessionName: 'Monday',
        workoutExerciseList: {
          1: {
            exerciseName: "Pull Ups",
            exerciseSets: {
              1: 20,
              2: 12,
              3: 8
            }
          },
          2: {
            exerciseName: "Pull Ups",
            exerciseSets: {
              1: 20,
              2: 12,
              3: 8
            }
          }
        }
      }
    }
  },
  {
    title: 'Kraftausdauer Teil 1',
    id: 2,
    workoutImg: ''
  },
  {
    title: 'Kraftausdauer Teil 2',
    id: 3,
    workoutImg: ''
  },
  {
    title: '7 Minuten Training',
    id: 4,
    workoutImg: ''
  },
  {
    title: 'Workout Zuhause',
    id: 5,
    workoutImg: ''
  }
]

For Example: A User have x Workouts. Each Workout have x Sessions (Monday, Wednesday, Friday). A session have also x Exercises with x sets.

The Problem: I want to modify this object and I have a few problems:

Problem: I want to add Sessions (Monday, Wednesday, Friday).

var session = {
        workoutSessionName: sessionName
      };

userWorkouts[1].push(session) 

Doesn't work, because its an object. Is there another way to "push" to an object?

like image 770
Fargho Avatar asked May 07 '26 17:05

Fargho


1 Answers

if i understood try to change to this.

userWorkouts: [
  {
    title: '3 Split',
    id: 1,
    workoutImg: '',
    workoutSessions: [{
     {
        workoutSessionName: 'Monday',
        workoutExerciseList: [{
          1: {
            exerciseName: "Pull Ups",
            exerciseSets: {
              1: 20,
              2: 12,
              3: 8
            }
          },
          2: {
            exerciseName: "Pull Ups",
            exerciseSets: {
              1: 20,
              2: 12,
              3: 8
            }
          }
        }
      }
      ]
    }
  },
  {
    title: 'Kraftausdauer Teil 1',
    id: 2,
    workoutImg: ''
  },
  {
    title: 'Kraftausdauer Teil 2',
    id: 3,
    workoutImg: ''
  },
  {
    title: '7 Minuten Training',
    id: 4,
    workoutImg: ''
  },
  {
    title: 'Workout Zuhause',
    id: 5,
    workoutImg: ''
  }
]

and use like this

var session = {
        workoutSessionName: sessionName
      };

userWorkouts[1].workoutSessions.push(session) 
like image 172
Astolfo Hoscher Avatar answered May 09 '26 05:05

Astolfo Hoscher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!