中断JSON对象并添加到表中

时间:2015-12-14 09:58:57

标签: javascript jquery html json

我需要从JSON对象中检索 sprintID issue_name 并将它们添加到表中。以下是我到目前为止所做的工作。

以下是一些示例JSON。

var mock_data = {
  "criticalList": [{
    "issue_id": 9,
    "local_id": 0,
    "issue_name": "issue_01",
    "project_id": 0,
    "estimated_time": 5,
    "dependents": [10, 11],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 11,
    "local_id": 0,
    "issue_name": "issue_03",
    "project_id": 0,
    "estimated_time": 5,
    "dependents": [15, 16, 14],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 14,
    "local_id": 0,
    "issue_name": "issue_06",
    "project_id": 0,
    "estimated_time": 6,
    "dependents": [21],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 21,
    "local_id": 0,
    "issue_name": "issue_12",
    "project_id": 0,
    "estimated_time": 4,
    "dependents": [29],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 29,
    "local_id": 0,
    "issue_name": "issue_20",
    "project_id": 0,
    "estimated_time": 6,
    "dependents": [34],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 34,
    "local_id": 0,
    "issue_name": "issue_25",
    "project_id": 0,
    "estimated_time": 3,
    "dependents": [36, 37],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 36,
    "local_id": 0,
    "issue_name": "issue_27",
    "project_id": 0,
    "estimated_time": 3,
    "dependents": [38],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 38,
    "local_id": 0,
    "issue_name": "issue_29",
    "project_id": 0,
    "estimated_time": 5,
    "dependents": [39],
    "localdependents": [],
    "critical": true
  }, {
    "issue_id": 39,
    "local_id": 0,
    "issue_name": "issue_30",
    "project_id": 0,
    "estimated_time": 4,
    "dependents": [40],
    "localdependents": [],
    "critical": true
  }],
  "sprints": [{
    "sprintID": "2015-12-01_2015-12-04",
    "issues": [{
      "issue_id": 9,
      "local_id": 0,
      "issue_name": "issue_01",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }, {
      "issue_id": 11,
      "local_id": 0,
      "issue_name": "issue_03",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [9],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }, {
      "issue_id": 10,
      "local_id": 0,
      "issue_name": "issue_02",
      "project_id": 0,
      "estimated_time": 3,
      "dependents": [9],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }, {
      "issue_id": 16,
      "local_id": 0,
      "issue_name": "issue_08",
      "project_id": 0,
      "estimated_time": 3,
      "dependents": [11],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }, {
      "issue_id": 15,
      "local_id": 0,
      "issue_name": "issue_07",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [11],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }, {
      "issue_id": 14,
      "local_id": 0,
      "issue_name": "issue_06",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [11],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-01_2015-12-04"
    }]
  }, {
    "sprintID": "2015-12-07_2015-12-11",
    "issues": [{
      "issue_id": 13,
      "local_id": 0,
      "issue_name": "issue_05",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [10],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 12,
      "local_id": 0,
      "issue_name": "issue_04",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [10],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 24,
      "local_id": 0,
      "issue_name": "issue_15",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [16],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 23,
      "local_id": 0,
      "issue_name": "issue_14",
      "project_id": 0,
      "estimated_time": 2,
      "dependents": [15],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 22,
      "local_id": 0,
      "issue_name": "issue_13",
      "project_id": 0,
      "estimated_time": 3,
      "dependents": [15],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 21,
      "local_id": 0,
      "issue_name": "issue_12",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [14],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 20,
      "local_id": 0,
      "issue_name": "issue_11",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [13],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }, {
      "issue_id": 19,
      "local_id": 0,
      "issue_name": "issue_10",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [12],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-07_2015-12-11"
    }]
  }, {
    "sprintID": "2015-12-14_2015-12-18",
    "issues": [{
      "issue_id": 18,
      "local_id": 0,
      "issue_name": "issue_09",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [12],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }, {
      "issue_id": 30,
      "local_id": 0,
      "issue_name": "issue_21",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [23, 24],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }, {
      "issue_id": 29,
      "local_id": 0,
      "issue_name": "issue_20",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [21, 22],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }, {
      "issue_id": 28,
      "local_id": 0,
      "issue_name": "issue_19",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [20],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }, {
      "issue_id": 27,
      "local_id": 0,
      "issue_name": "issue_18",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [19],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }, {
      "issue_id": 26,
      "local_id": 0,
      "issue_name": "issue_17",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [19],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-14_2015-12-18"
    }]
  }, {
    "sprintID": "2015-12-21_2015-12-25",
    "issues": [{
      "issue_id": 25,
      "local_id": 0,
      "issue_name": "issue_16",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [18],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-21_2015-12-25"
    }, {
      "issue_id": 34,
      "local_id": 0,
      "issue_name": "issue_25",
      "project_id": 0,
      "estimated_time": 3,
      "dependents": [29, 30],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-21_2015-12-25"
    }, {
      "issue_id": 33,
      "local_id": 0,
      "issue_name": "issue_24",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [27, 28],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-21_2015-12-25"
    }, {
      "issue_id": 32,
      "local_id": 0,
      "issue_name": "issue_23",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [26],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-21_2015-12-25"
    }]
  }, {
    "sprintID": "2015-12-28_2015-12-31",
    "issues": [{
      "issue_id": 31,
      "local_id": 0,
      "issue_name": "issue_22",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [25],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }, {
      "issue_id": 36,
      "local_id": 0,
      "issue_name": "issue_27",
      "project_id": 0,
      "estimated_time": 3,
      "dependents": [32, 33, 34],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }, {
      "issue_id": 35,
      "local_id": 0,
      "issue_name": "issue_26",
      "project_id": 0,
      "estimated_time": 2,
      "dependents": [31],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }, {
      "issue_id": 38,
      "local_id": 0,
      "issue_name": "issue_29",
      "project_id": 0,
      "estimated_time": 5,
      "dependents": [35, 36],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }, {
      "issue_id": 37,
      "local_id": 0,
      "issue_name": "issue_28",
      "project_id": 0,
      "estimated_time": 6,
      "dependents": [34],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }, {
      "issue_id": 39,
      "local_id": 0,
      "issue_name": "issue_30",
      "project_id": 0,
      "estimated_time": 4,
      "dependents": [37, 38],
      "localdependents": [],
      "critical": false,
      "sprintID": "2015-12-28_2015-12-31"
    }]
  }]
};

这是不起作用的循环。

$(mock_data.sprints).each(function(i, value) {
  $(sprints.value).each(function(j, val) {
    $('.list').append('<tr>' +
      '<td>' + (i + 1) + '</td>' +
      '<td>' + val.sprintID + '</td>' +
      '<td>' + val.issue_name + '</td>' +
      '</tr>');
  });
});

HTML就是这样,<table class="list"></table>

1 个答案:

答案 0 :(得分:6)

问题是内部循环试图访问未定义的变量rmarkdown。您需要做的就是更正脚本,在第二个循环中将maximumBillingTier更改为sprints.value,以迭代sprints.value数组中的对象。

value.issues

&#13;
&#13;
issues
&#13;
$(mock_data.sprints).each(function(i, value) {
    $(value.issues).each(function(j, val) {
        $('.list').append('<tr>' +
                          '<td>' + (i + 1) + '</td>' +
                          '<td>' + val.sprintID + '</td>' +
                          '<td>' + val.issue_name + '</td>' +
                          '</tr>');
    });
});
&#13;
var mock_data = {sprints:[{issues:[{issue_name:'issue_01',sprintID:'2015-12-01_2015-12-04'},{issue_name:'issue_03',sprintID:'2015-12-01_2015-12-04'},{issue_name:'issue_02',sprintID:'2015-12-01_2015-12-04'},{issue_name:'issue_08',sprintID:'2015-12-01_2015-12-04'},{issue_name:'issue_07',sprintID:'2015-12-01_2015-12-04'},{issue_name:'issue_06',sprintID:'2015-12-01_2015-12-04'}]},{issues:[{issue_name:'issue_05',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_04',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_15',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_14',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_13',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_12',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_11',sprintID:'2015-12-07_2015-12-11'},{issue_name:'issue_10',sprintID:'2015-12-07_2015-12-11'}]},{issues:[{issue_name:'issue_09',sprintID:'2015-12-14_2015-12-18'},{issue_name:'issue_21',sprintID:'2015-12-14_2015-12-18'},{issue_name:'issue_20',sprintID:'2015-12-14_2015-12-18'},{issue_name:'issue_19',sprintID:'2015-12-14_2015-12-18'},{issue_name:'issue_18',sprintID:'2015-12-14_2015-12-18'},{issue_name:'issue_17',sprintID:'2015-12-14_2015-12-18'}]},{issues:[{issue_name:'issue_16',sprintID:'2015-12-21_2015-12-25'},{issue_name:'issue_25',sprintID:'2015-12-21_2015-12-25'},{issue_name:'issue_24',sprintID:'2015-12-21_2015-12-25'},{issue_name:'issue_23',sprintID:'2015-12-21_2015-12-25'}]},{issues:[{issue_name:'issue_22',sprintID:'2015-12-28_2015-12-31'},{issue_name:'issue_27',sprintID:'2015-12-28_2015-12-31'},{issue_name:'issue_26',sprintID:'2015-12-28_2015-12-31'},{issue_name:'issue_29',sprintID:'2015-12-28_2015-12-31'},{issue_name:'issue_28',sprintID:'2015-12-28_2015-12-31'},{issue_name:'issue_30',sprintID:'2015-12-28_2015-12-31'}]}]};

$(mock_data.sprints).each(function(i, value) {
  $(value.issues).each(function(j, val) {
    $('.list').append('<tr>' +
                      '<td>' + (i + 1) + '</td>' +
                      '<td>' + val.sprintID + '</td>' +
                      '<td>' + val.issue_name + '</td>' +
                      '</tr>');
  });
});
&#13;
&#13;
&#13;