Deferred assignment in pure javascript
<p>In <a href="https://codereview.stackexchange.com/questions/115487/parsing-and-calculating-data">this question</a> I encountered the following simplified problem:</p>

<p>We start with an array of Objects with a value attribute. We want to calculate for each value what percentage of the sum of values it is, and add it to the structure as a property. To do this, we need to know the sum of values, but this sum is not calculated beforehand.</p>

<pre><code>//Original data structure
[
  { "value" : 123456 },
  { "value" : 12146  }
]

//Becomes
[
  { 
    "value" : 123456,
    "perc"  : 0.9104
  },
  {
    "value" : 12146 ,
    "perc"  : 0.0896
  }
]
</code></pre>

<hr>

<p>An easy, and probably most readable, solution is to go through the data structure twice. First we calculate the sum, then we calculate the percentage and add it to the data structure. </p>

<pre><code>var i;
var sum = 0;
for( i = 0; i &lt; data.length; i++ ) {
  sum += data[i].value;
}
for( i = 0; i &lt; data.length; i++ ) {
  data[i].perc = data[i].value / sum;
}
</code></pre>

<hr>

<p>Can we instead just go through the data structure once, and somehow tell that the percentage expression should only be evaluated once the entire sum is known?</p>

<p><em>I am primarily interested in answers that address pure javascript. That is: Without any libraries.</em></p>