Reputation: 1277
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?
Upvotes: 3
Views: 197
Reputation: 2153
Not sure you need to use 1. array or 2. object for workoutSessions
workoutSessions
instead of object because you are already using an integer as a key and then just add using push()
angular.extend
https://docs.angularjs.org/api/ng/function/angular.extendHere is a plunk for option 2: https://plnkr.co/edit/x0isJdzXHq2gX7vfsQdG?p=preview
Upvotes: 1
Reputation: 262
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)
Upvotes: 3