2016-03-18 105 views
4

目前我正在研究一个离子应用程序(angular1),我正在研究一个巨大的对象。JavaScript对象操作问题

该对象将看起来像这样:

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: '' 
    } 
] 

对于实施例:A用户让X锻炼。每次锻炼都有x次会话(周一,周三,周五)。一个会话也有x个练习x组。

问题:我想修改该对象和我有几个问题:

问题:我想补充会议(星期一,星期三,星期五)。

var session = { 
     workoutSessionName: sessionName 
     }; 

userWorkouts[1].push(session) 

不起作用,因为它是一个对象。有没有另一种方式来“推”到一个对象?

+1

是否有问题2或者是一个错误? – User0123456789

+1

为什么你有递增整数的对象作为键?为什么不使用数组? – Hamms

+0

同意。 'workoutSessions','workoutExerciseList'和'exerciseSets'都应该被定义为对象数组,而不是对象。 – Lex

回答

3

如果我明白尝试改变这一点。

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: '' 
    } 
] 

和使用这样

var session = { 
     workoutSessionName: sessionName 
     }; 

userWorkouts[1].workoutSessions.push(session)