Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate over JSON array
    text
    copied!<p>I've read several examples of how to do what I want but none seem to work. I want to iterate over a JSON array but it's not working for me. When I look in chromes js console my data looks like this:</p> <pre><code>"[\r\n {\r\n \"EntryId\": 3,\r\n \"Title\": \"Tiny Living For sales\",\r\n \"Description\": \"This is a house for sale\",\r\n \"AddressViewModel\": {\r\n \"AddressId\": 3,\r\n \"Street1\": null,\r\n \"Street2\": null,\r\n \"City\": \"Los Angeles\",\r\n \"LocationId\": 5,\r\n \"LocationName\": \"California\",\r\n \"PostalCode\": null,\r\n \"Phone\": null,\r\n \"Latitude\": 34.052234,\r\n \"Longitude\": -118.243685\r\n },\r\n \"EntryCategoryName\": \"Houses for Sale\",\r\n \"EventStartDate\": null,\r\n \"EventEndDate\": null\r\n },\r\n {\r\n \"EntryId\": 2,\r\n \"Title\": \"Tiny Living Workshop\",\r\n \"Description\": \"This is a workshop\",\r\n ... </code></pre> <p>And if I turn it into an object by doing so:</p> <pre><code>var myObject = eval('(' + locations + ')'); </code></pre> <p>It looks like this (formated):</p> <pre><code>[ { "EntryId": 3, "Title": "Tiny Living For sales", "Description": "This is a house for sale", "AddressViewModel": { "AddressId": 3, "Street1": null, "Street2": null, "City": "Los Angeles", "LocationId": 5, "LocationName": "California", "PostalCode": null, "Phone": null, "Latitude": 34.052234, "Longitude": -118.243685 }, "EntryCategoryName": "Houses for Sale", "EventStartDate": null, "EventEndDate": null }, { "EntryId": 2, "Title": "Tiny Living Workshop", </code></pre> <p>...</p> <p>But when I try to iterate over it (either the raw JSON or the object) it gives me each letter of the JSON, not each object in the array</p> <pre><code>for (var i = 0; i &lt; myObject.length; i++) { console.log(myObject[i]); } </code></pre> <p>Like so:</p> <pre><code> " [ \ r \ </code></pre> <p>Thanks</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