Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>For #1:</strong> The best setup here would be to have your <code>pcdata</code> in a key-value data format. I'll call that <code>pchash</code>. Then you can use this (relatively) simple syntax:</p> <pre><code>for(var i = 0; i &lt; chdata.length; ++i) { var ch = chdata[i]; var dist = pchash[ch.pcaid].distance; // Other stuff... } </code></pre> <p>To transition your current <code>pcdata</code> into <code>pchash</code>, you can use something like this:</p> <pre><code>pchash = {}; for(var i = 0; i &lt; pcdata.length; ++i) { pchash[pcdata[i]['YourID']] = pcdata[i]; } </code></pre> <p>Or, better yet and if you control the server, have <code>pcdata</code> sent over in that format to start with.</p> <p><strong>For #2:</strong> JavaScript arrays have a built-in <a href="http://www.w3schools.com/jsref/jsref_sort.asp" rel="nofollow">sort</a> function, and you can pass in a comparator for custom sorting. In your case, try:</p> <pre><code>whatever.sort(function(a, b) { return a.Distance - b.Distance; }); </code></pre> <p>If that gives you the reverse of what you want, just subtract <code>a.Distance</code> from <code>b.Distance</code> instead.</p> <p>Hope that helps!</p> <p><strong>PS:</strong> Another answer suggests brute-forcing this with a nested loop. If you have any significant amount of data, I wouldn't recommend it: It'll take <code>chdata.length * pcdata.length</code> iterations, while this will only take <code>chdata.length + pcdata.length</code> iterations (or just <code>chdata.length</code>, if you get the key-value format from the server). But on small data sets, the difference won't be noticeable.</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