当前位置:  开发笔记 > 编程语言 > 正文

中断JSON对象并添加到表中

如何解决《中断JSON对象并添加到表中》经验,为你挑选了1个好方法。

我需要从JSON对象中检索sprintIDissue_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('' +
      '' + (i + 1) + '' +
      '' + val.sprintID + '' +
      '' + val.issue_name + '' +
      '');
  });
});

HTML是这样的,



1> 小智..:

问题是内部循环试图访问未定义的变量sprints.value.所有你需要做的纠正脚本,改sprints.valuevalue.issues在第二循环中的对象遍历的issues数组.

$(mock_data.sprints).each(function(i, value) {
    $(value.issues).each(function(j, val) {
        $('.list').append('' +
                          '' + (i + 1) + '' +
                          '' + val.sprintID + '' +
                          '' + val.issue_name + '' +
                          '');
    });
});

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('' +
                      '' + (i + 1) + '' +
                      '' + val.sprintID + '' +
                      '' + val.issue_name + '' +
                      '');
  });
});
body {
  font: 1em/1.5 sans-serif;
}
table {
  border-collapse: collapse;
}
td,
th {
  padding: .5em;
  border: thin solid #ccc
}

Index Sprint Id Issue name
推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有