Note that there are some explanatory texts on larger screens.

plurals
  1. POjs - How to change one key's value based on another key's value in array of objects
    text
    copied!<p>I have an array of objects like this:</p> <pre><code>var chartData = [{count: 0, idTag: "24"} {count: 0, idTag: "25"} {count: 0, idTag: "26"}] </code></pre> <p>My code does some work and generates a number (<code>totalValue</code>). If <code>totalValue</code> matches the <code>idTag</code>, its <code>count</code> gets increased by one:</p> <pre><code>for (i=0; i&lt;timesFlipped; i++) { totalValue = runTrial(); for (j=0; j&lt;chartData.length; j++) { if (chartData[j].idTag === totalValue) { chartData[j].count += 1; break; } } } </code></pre> <p>This is a simple example, but <code>chartData</code> could hold a few dozen objects, and <code>timesFlipped</code> can be hundreds of thousands of iterations.</p> <p>Is this the best way to change the <code>count</code> value based on matching the <code>idTag</code> value? Is there a way to just find the index of the array with the correct <code>idTag</code> value without iterating over the whole array of objects?</p> <p>Here's the page I'm working on if you want more context: <a href="http://blue.butler.edu/~aazman/coupling/" rel="nofollow">http://blue.butler.edu/~aazman/coupling/</a></p> <hr> <p><strong>Update:</strong></p> <p>Thanks for all the suggestions. I kinda formed my own solution based on what I saw. A bit more background about the project which led me to my solution:</p> <ul> <li>The length of <code>chartData</code> and the min idTag value are calculated based on user inputs when they click submit</li> <li>Once <code>min</code> and <code>length</code> are calculated (thus the max idTag is known), <code>chartData</code> is iteratively initialized:</li> </ul> <p><code>var min = 23-5*numNickel-numPenny; //numNickel &amp; numPenny are the user inputs</code></p> <p><code>var length = 5*(2*numNickel+1)+2*numPenny;</code></p> <p><code>var tempTag = min;</code></p> <p><code>for (j=0;j&lt;length;j++) {</code></p> <p><code>chartData[j] = {count: 0, idTag = tempTag};</code></p> <p><code>tempTag++;</code></p> <p><code>}</code></p> <ul> <li>For reasons particular to the rules of the game, the idTag values (however many there are) are symmetrically centered around <code>idTag:25</code> (e.g. idTags = {24, 25, 26} or idTags = {20,21,22,23,24,25,26,27,28,29,30}. No matter the length of <code>chartData</code>, the length will always be odd and <code>idTag:25</code> will always be the <code>[(chartData.length-1)/2]</code>'th index.</li> </ul> <p>Thus, if the idTags for <code>chartData</code> are <code>{20,21,22,23,24,25,26,27,28,29,30}</code> and if <code>runTrial()</code> gives <code>totalValue = 22</code>, then <code>idTag:22</code> will always be at index <code>(totalValue - min)</code>.</p> <p>So I changed the initialization of chartData and the way the count gets increased to:</p> <pre><code>for (j=0; j&lt;length; j++) { chartData[j] = {count: 0}; //based on what I'm doing with chartData next, I need each index to still be an object } for (i=0; i&lt;timesFlipped; i++) { totalValue = runTrial(); var index = totalValue - min; chartData[index].count++; } </code></pre> <p>So thanks all for your help :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload