title: How can I retrieve ko.computed running sum of foreach when iterating through observablearray in knockout.js
body: <p>I have a list of ProjectPeriod items as an observableArray in a knockout viewModel which includes the number of months in each period. I want to display the end date for each row in the foreach. Currently I am using a ko.computed value in the viewModel, but I'm not able to loop over each item up to the item being displayed. How can I loop over and sum the values being displayed only up to the current item in the foreach?</p>
  
  <p>Currently I have the following HTML:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr class="tableHeader"&gt;
          &lt;th&gt;Period&lt;/th&gt;
          &lt;th&gt;Number of Months&lt;/th&gt;
          &lt;th&gt;End of Period&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tbody data-bind="foreach: ProjectPeriod"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;input data-bind="value: ProjectYearText" /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input data-bind="value: PeriodMonths" /&gt;&lt;/td&gt;
              &lt;td data-bind="text: endDate"&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>and the following viewModel:</p>
  
  <pre><code>function ProjectPeriod(projectYearId, projectYearText, periodMonths, viewModel) {
      var self = this;
          self.ProjectYearId = projectYearId;
          self.ProjectYearText = ko.observable(projectYearText);
          self.PeriodMonths = ko.observable(periodMonths);
          self.viewModel = viewModel;
  
          self.endDate = ko.computed(function () {
              var startDate = hfProjectDates.Get("ProjectStartDate");
  
              // Calculate the number of months from the beginning to the current period.
              var monthCount = 0;
              ko.utils.arrayForEach(self.viewModel.ProjectPeriods(), function (projectPeriod) {
                  if (projectPeriod.ProjectYearId &lt; self.ProjectYearId)
                      monthCount += projectPeriod.PeriodMonths;
                  });
  
              var endDate = moment(startDate).add('M', monthCount);
              return endDate ? endDate.format("M/DD/YYYY") : "None";
          });
      }
  
      function ProjectPeriodViewModel() {
          // Data
          var self = this;
  
          self.ProjectPeriods = ko.observableArray([
              new ProjectPeriod(1, "1st Year", 12, ProjectPeriodViewModel),
              new ProjectPeriod(2, "2nd Year", 12, ProjectPeriodViewModel),
              new ProjectPeriod(3, "3rd Year", 12, ProjectPeriodViewModel)
          ]);
  
      }
  </code></pre>
  
  <p>I am really just getting started with Knockout, so I expect there are more than a few issues with the way I'm approaching this. But specifically I need to get the running end date to display.</p>
  
  <p><strong>Update:</strong> based on Matt;s feedback I've updated to include the observable in ProjectPeriod, but I'm running into issues getting the reference from the viewModel and iterating over the array.</p>