JQuery $ .each()JSON数组对象迭代

时间:2021-04-21 20:13:52

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

我在尝试解决JQuery $ .each()迭代时遇到了一些麻烦

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

我的第一个$ .each迭代非常有效,但“EVENTS”的子迭代是我遇到问题的地方

My first $.each() function

我的第一个$ .each()函数

     $.each(json, function(key) {

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

     ....

My non-working second $.each() function

我的非工作第二个$ .each()函数

     $.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

我(松散地)理解这不是一个单一的JSON对象,而是一个JSON对象数组,但不理解第一个版本是否有效,为什么第二个版本没有

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()中的$ .each(),我想要实现的最终结果,我知道下面的代码不起作用,而且很可能是愚蠢的,但是让我知道我想要尝试什么实现:通过父级迭代父级然后子级

$.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+'">';
 ...

1 个解决方案

#1


38  

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函数()分配第二个变量,使它更容易,因为它将为您提供数据(因此您不必使用指标)。

$.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/

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

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

编辑重命名变量,使其更容易理解什么是什么。

#1


38  

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函数()分配第二个变量,使它更容易,因为它将为您提供数据(因此您不必使用指标)。

$.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/

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

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

编辑重命名变量,使其更容易理解什么是什么。