Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The JSON you provided is invalid, but, assuming the JSON looks like this (based on your current iteration code):</p> <pre><code>var results = [{ "name": "name1", "type": [{ "id": '2'}, { "id": '1'}, { "id": '4'}, { "id": '6'}], "images": [{ "url": "url0" }, { "url": "url1" }, { "url": "url2" }, { "url": "url3" }, { "url": "url4" }], }, { "name": "name2", "type": [{ "id": '25' }, { "id": '123' }, { "id": '423' }, { "id": '346' }], "images": [{ "url": "url0" }, { "url": "url1" }, { "url": "url2" }, { "url": "url3" }, { "url": "url4" }], }, { "name": "name3", "type": null, "images": null }]; </code></pre> <p>You have several problems with this statement:</p> <pre><code>for(var i = 0; i &lt; results.length; i++){ $('.results')append('&lt;p class="liste"&gt;'+results[i].name+'&lt;/p&gt;&lt;ul&gt;&lt;/ul&gt;'); $('.liste').prepend('&lt;img src="'+results[i].images[1].url+'"'); for(var j = 0; j &lt; results[i].type.length; j++){ $('.results ul').append('&lt;li&gt;'+results[i].type[j].id+'&lt;/li&gt;'); } } </code></pre> <ul> <li>$('.liste').prepend will append the provided element to <strong>ALL</strong> elements with a class of <code>.liste</code>. Each time you prepend a new image, all existing <code>&lt;p&gt;</code> elements will get it.</li> <li>$('.results ul').append - see above comment. Each time you append, all existing elements that match will get additional, unwanted values attached</li> <li>Both the <code>type</code> and <code>images</code> values can be null and you haven't provided any guards against this in your loop.</li> </ul> <p>Something like this given the assumption above will work better for you:</p> <p><a href="http://jsfiddle.net/davidcl64/gm78r/" rel="nofollow">Demo Fiddle</a></p> <p>Code:</p> <pre><code>// Use a document fragment to avoid DOM updates in the loop var $fragment = $(document.createDocumentFragment()); for (var i = 0; i &lt; results.length; i++) { // If there is an image, add it if(results[i].images) { $fragment.append($('&lt;img src="' + results[i].images[1].url + '"/&gt;')); } $fragment.append($('&lt;p class="liste"&gt;' + results[i].name + '&lt;/p&gt;')); // Keep track of the current UL so it can be appended to directly var $curULTag = $('&lt;ul&gt;&lt;/ul&gt;').appendTo($fragment); if(results[i].images &amp;&amp; results[i].type) { for (var j = 0; j &lt; results[i].type.length; j++) { // add the li to the current UL $curULTag.append('&lt;li&gt;' + results[i].type[j].id + '&lt;/li&gt;'); } } } // Append the full results $('.results').append($fragment); </code></pre> <p>Results:</p> <pre><code>&lt;div class="results"&gt; &lt;img src="url1"&gt; &lt;p class="liste"&gt;name1&lt;/p&gt; &lt;ul&gt; &lt;li&gt;2&lt;/li&gt; &lt;li&gt;1&lt;/li&gt; &lt;li&gt;4&lt;/li&gt; &lt;li&gt;6&lt;/li&gt; &lt;/ul&gt; &lt;img src="url1"&gt; &lt;p class="liste"&gt;name2&lt;/p&gt; &lt;ul&gt; &lt;li&gt;25&lt;/li&gt; &lt;li&gt;123&lt;/li&gt; &lt;li&gt;423&lt;/li&gt; &lt;li&gt;346&lt;/li&gt; &lt;/ul&gt; &lt;p class="liste"&gt;name3&lt;/p&gt; &lt;ul&gt;&lt;/ul&gt; &lt;/div&gt; </code></pre> <p>If my assumption about the JSON structure you provided isn't correct, you can adjust the above code to provide the results you need.</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