Note that there are some explanatory texts on larger screens.

plurals
  1. POloop through JSON with null objects
    text
    copied!<p>I have this problem with my JSON file.</p> <pre><code>{"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>the JSON object looks like this When i loop through the it kicks me out when it comes to the null</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> <p>the result should look somewhat like this</p> <pre><code>&lt;div class="results"&gt; &lt;img src="url1"&gt;&lt;p&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&gt;name2&lt;/p&gt; &lt;ul&gt; &lt;/ul&gt; &lt;img src="url1"&gt;&lt;p&gt;name3&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;/div&gt; </code></pre> <p>i tryed</p> <pre><code>if(results[i].images != null){ $('.liste').prepend('&lt;img src="'+results[i].images[1].url+'"&gt;'); }else{ $('.liste').prepend('&lt;img src="some default image" width="170" height="170"&gt;'); } if(results[i].type != null){ for (var j = 0; j &lt; results[i].type.length.length; j++) { $('.album').append('&lt;li&gt;'+[i]+'-'+[j]+'-'+results[i].type.length[j].id+'&lt;/li&gt;'); }; </code></pre> <p>it gehts really messy the image comes for every [i] and the first [k] appears as it suppose to be as well as last [k] in in the pre. [i]</p> <pre><code>[i][j] name1 [0][0] [0][1] [0][2] [0][3] [1][0] name2 [1][0] ...... </code></pre> <p>I hope some one can help me with that</p> <p><strong>EDIT</strong> I forgot every "type" is listed under each name.</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