Note that there are some explanatory texts on larger screens.

plurals
  1. POMerging new data into existing array in jQuery
    text
    copied!<p>I have this array which I create from some data stored in a database:</p> <pre><code>var chdata = [ {title: "title one",link: "link one",thumb: "image url one",pcaid: "0001"}, {title: "title two",link: "link two",thumb: "image url two",pcaid: "0002"}, {title: "title three",link: "link three",thumb: "image url three",pcaid: "0003"}, {title: "title four",link: "link four",thumb: "image url four",pcaid: "0004"} ]; </code></pre> <p>then I pull back some location data from a geocoding service which arrives in this format:</p> <pre><code>var pcdata = [ {Distance: "12.2",Latitude: "50.1",Longitude: "0.1",YourID:"0003"}, {Distance: "80.7",Latitude: "50.4",Longitude: "0.5",YourID:"0001"}, {Distance: "95.3",Latitude: "50.9",Longitude: "0.7",YourID:"0004"}, {Distance: "135.1",Latitude: "51.3",Longitude: "0.9",YourID:"0002"}, ] </code></pre> <p>The 'pcaid' in chdata and 'YourID' in pcdata are the foreign keys which relate the two arrays if you'll excuse the DBA analogy. </p> <p>I need to 'each()' through the chdata array, displaying data from it, and the distance of the related item in pcdata. For example in pseudo code:</p> <pre><code>&lt;a href="link one"&gt; &lt;img src="image url one" /&gt;&lt;br /&gt;title one (80.7 miles away) &lt;/a&gt; </code></pre> <p>This data needs to be in distance order. My questions are:</p> <ol> <li>How do I relate/intersect <code>chdata</code> and <code>pcdata</code> based on a match between <code>pcaid</code> and <code>YourID</code>.</li> <li>Once this is done, I plan to use array.sort to get it into Distance order. Do you foresee any problems with this.</li> </ol>
 

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