Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 'each' loop with JSON array

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?

like image 592
Ben Avatar asked Jun 12 '10 22:06

Ben


People also ask

How to loop through json array in jQuery?

each() or $. each() Function. This is the simplest way of looping around an array or a JSON array in JavaScript or jQuery.

How to loop through array in 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.

How to break the each loop in jQuery?

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.


2 Answers

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);         });     }); });  
like image 145
karim79 Avatar answered Sep 29 '22 20:09

karim79


Brief code but full-featured

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

alt text

More Info
The JSON data was used in the following way:

for each category (key name the array is held under):

  • the key name is used as the section heading (e.g. justIn)

for each object held inside an array:

  • 'text' becomes the content of a div
  • 'textType' becomes the class of the div (hooked into a style sheet)
  • 'textId' becomes the id of the div
  • e.g. <div id="T122" class="Order">Come over here</div>
like image 28
John K Avatar answered Sep 29 '22 19:09

John K