I'm trying to use jQuery's each
loop to go through this JSON and add it to a div
named #contentHere
. The JSON is as follows:
{ "justIn": [ { "textId": "123", "text": "Hello", "textType": "Greeting" }, { "textId": "514", "text":"What's up?", "textType": "Question" }, { "textId": "122", "text":"Come over here", "textType": "Order" } ], "recent": [ { "textId": "1255", "text": "Hello", "textType": "Greeting" }, { "textId": "6564", "text":"What's up?", "textType": "Question" }, { "textId": "0192", "text":"Come over here", "textType": "Order" } ], "old": [ { "textId": "5213", "text": "Hello", "textType": "Greeting" }, { "textId": "9758", "text":"What's up?", "textType": "Question" }, { "textId": "7655", "text":"Come over here", "textType": "Order" } ] }
I'm getting this JSON through use of this code:
$.get("data.php", function(data){ })
Any solutions?
each() or $. each() Function. This is the simplest way of looping around an array or a JSON array in JavaScript or jQuery.
Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.
To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.
Try (untested):
$.getJSON("data.php", function(data){ $.each(data.justIn, function() { $.each(this, function(k, v) { alert(k + ' ' + v); }); }); $.each(data.recent, function() { $.each(this, function(k, v) { alert(k + ' ' + v); }); }); $.each(data.old, function() { $.each(this, function(k, v) { alert(k + ' ' + v); }); }); });
I figured, three separate loops since you'll probably want to treat each dataset differently (justIn, recent, old). If not, you can do:
$.getJSON("data.php", function(data){ $.each(data, function(k, v) { alert(k + ' ' + v); $.each(v, function(k1, v1) { alert(k1 + ' ' + v1); }); }); });
The following is a hybrid jQuery solution that formats each data "record" into an HTML element and uses the data's properties as HTML attribute values.
The jquery each
runs the inner loop; I needed the regular JavaScript for
on the outer loop to be able to grab the property name (instead of value) for display as the heading. According to taste it can be modified for slightly different behaviour.
This is only 5 main lines of code but wrapped onto multiple lines for display:
$.get("data.php", function(data){ for (var propTitle in data) { $('<div></div>') .addClass('heading') .insertBefore('#contentHere') .text(propTitle); $(data[propTitle]).each(function(iRec, oRec) { $('<div></div>') .addClass(oRec.textType) .attr('id', 'T'+oRec.textId) .insertBefore('#contentHere') .text(oRec.text); }); } });
Produces the output
(Note: I modified the JSON data text values by prepending a number to ensure I was displaying the proper records in the proper sequence - while "debugging")
<div class="heading"> justIn </div> <div id="T123" class="Greeting"> 1Hello </div> <div id="T514" class="Question"> 1What's up? </div> <div id="T122" class="Order"> 1Come over here </div> <div class="heading"> recent </div> <div id="T1255" class="Greeting"> 2Hello </div> <div id="T6564" class="Question"> 2What's up? </div> <div id="T0192" class="Order"> 2Come over here </div> <div class="heading"> old </div> <div id="T5213" class="Greeting"> 3Hello </div> <div id="T9758" class="Question"> 3What's up? </div> <div id="T7655" class="Order"> 3Come over here </div> <div id="contentHere"></div>
Apply a style sheet
<style> .heading { font-size: 24px; text-decoration:underline } .Greeting { color: green; } .Question { color: blue; } .Order { color: red; } </style>
to get a "beautiful" looking set of data
More Info
The JSON data was used in the following way:
for each category (key name the array is held under):
for each object held inside an array:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With