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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6298599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:39:31  来源:igfitidea点击:

JQuery $.each() JSON array object iteration

jqueryjsoneach

提问by Jay Rizzi

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

回答by Niklas

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

也为 the 分配第二个变量$.each function(),使它更容易,因为它会为您提供数据(因此您不必使用索引)。

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

editrenamed the variables to make it bit easier to understand what is what.

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