title: create a JavaScript object dynamically
body: <p>I want to create a calendar using <code>AngularJS</code>, and my model is an object like this:</p>
  
  <pre><code>$scope.model = {
          weeks: [
                  {
                      days: [
                      null,
                      {
                          name: "3 dec",
                          toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]
                      },
                      {
                          name: "4 dec",
                          toDoItems: [{name: "Task 1"}, {name: "Task 2"}]
                      }
                  ]
                  },
                  {
                      days: [
                      null,
                      {
                          name: "5 dec",
                          toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]
                      },
                      {
                          name: "6 dec",
                          toDoItems: [{name: "Task 1"}, {name: "Task 2"}]
                      }
                  ]
                  }
  
              ]
             }
  </code></pre>
  
  <p>But I want to create the object dynamically.</p>
  
  <p>I've tried something like this, but it gives me the following error: </p>
  
  <pre><code>TypeError: Cannot call method 'push' of undefined
  
  
  
   $scope.fillMonth = function () {
          var gap = dayInWeek($scope.year.value, $scope.month, 1),
              nrOfDays = daysInMonth($scope.year.value, $scope.month);
  
          $scope.model = {};
  
          for (var i = 0; i &lt; (nrOfDays + gap) % 7; i++) {
              for (var j = 0; j &lt; 7; j++) {
                  if (j === 0)
                      $scope.model.weeks.push([]);
  
                  if (i === 0 &amp;&amp; j &lt; gap)
                      $scope.model.weeks[i].days.push(null);
  
                  else
                      $scope.model.weeks[i].days.push([{ name: i + ' ' + j, toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]}]);
              }
          }
      }
  </code></pre>
  
  <p>Can anyone help me with this? Thanks in advance!</p>