[jquery] JQuery $.each() JSON array object iteration

I am having some real difficulty attempting to solve a JQuery $.each() iteration

This is my array, limiting results for convenience

[{"GROUP_ID":"143",
  "GROUP_TYPE":"2011 Season",
  "EVENTS":[
    {"EVENT_ID":"374","SHORT_DESC":"Wake Forest"},
    {"EVENT_ID":"376","SHORT_DESC":"Yale"},
    {"EVENT_ID":"377","SHORT_DESC":"Michigan State"}]
 },
 {"GROUP_ID":"142",
  "GROUP_TYPE":"2010 Season",
  "EVENTS":[
    {"EVENT_ID":"370","SHORT_DESC":"Duke"},
    {"EVENT_ID":"371","SHORT_DESC":"Northwestern"},
    {"EVENT_ID":"372","SHORT_DESC":"Brown"}]
}]

My first $.each iteration works very well, but the sub iteration for "EVENTS" is where I am having issues

My first $.each() function

     $.each(json, function(key) {

            html = '<a href="'+json[key].GROUP_ID+'">';

     ....

My non-working second $.each() function

     $.each(json.EVENTS, function(key) {
    newHTML += '<p>'+json.EVENTS[key].SHORT_DESC+'</p>';


     ...

I am understanding (loosely) that this is not a singular JSON object, but a JSON array of objects, but not understanding if the first version works why the second does not

the end result I want to achieve once I understand this is an $.each() within an $.each(), I know the code below does not work, and more than likely idiotic, but gives an idea of what im trying to achieve : iterate through parent then child by parent

$.each(json, function(key) {

            html = '<a href="'+json[key].GROUP_ID+'">';

     $.each(json[key].EVENTS, function(subkey) {

            html = '<a href="'+json[key]EVENTS[subkey].SHORT_DESC+'">';
 ...

This question is related to jquery json each

The answer is


Assign the second variable for the $.each function() as well, makes it lot easier as it'll provide you the data (so you won't have to work with the indicies).

$.each(json, function(arrayID,group) {
            console.log('<a href="'+group.GROUP_ID+'">');
    $.each(group.EVENTS, function(eventID,eventData) {
            console.log('<p>'+eventData.SHORT_DESC+'</p>');
     });
});

Should print out everything you were trying in your question.

http://jsfiddle.net/niklasvh/hZsQS/

edit renamed the variables to make it bit easier to understand what is what.


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to each

jQuery animated number counter from zero to value How to iterate over array of objects in Handlebars? Excel VBA For Each Worksheet Loop jQuery looping .each() JSON key/value not working jQuery '.each' and attaching '.click' event How to use continue in jQuery each() loop? jQuery .each() with input elements C++ for each, pulling from vector elements jQuery each loop in table row Get href attribute on jQuery