有一个JavaScript对象,我需要检索free_time
,done_ratio
,criticalTask
,dependency
每个任务元素.
这就是我所做的,但它不起作用.
var mock_data_allocation = {"allocation":[{"id":7,"name":"Manoj D","limit":4.0,"available_time":16.0,"tasks":[{"id":34,"name":"issue_25","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":3,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":true,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Manoj D","dependency":[]},{"id":36,"name":"issue_27","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":3,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":true,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Manoj D","dependency":[33,34]},{"id":38,"name":"issue_29","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":5,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":true,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Manoj D","dependency":[35,36]},{"id":39,"name":"issue_30","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":4,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":true,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Manoj D","dependency":[37,38]}]},{"id":5,"name":"Thisun H","limit":4.0,"available_time":16.0,"tasks":[{"id":33,"name":"issue_24","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":5,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":false,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Thisun H","dependency":[]},{"id":35,"name":"issue_26","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":2,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":false,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Thisun H","dependency":[]},{"id":37,"name":"issue_28","projectId":3,"startDate":"2015-12-28","dueDate":"2015-12-31","done_ratio":0,"estimate":6,"remaining":0,"sprintId":"2015-12-28_2015-12-31","criticalTask":false,"assigned":true,"tracker":"Bug","status":"New","assignedTo":"Thisun H","dependency":[34]}]}]};
$(mock_data_allocation.allocation).each( function(i, value){
$('.taskdetail').append('' +
''+ (i+1) +' '+
''+ value.tasks.name +' '+
''+value.tasks.free_time+' '+
''+value.tasks.done_ratio+' '+
''+value.tasks.criticalTask+' '+
''+value.tasks.dependency.length+' '+
' ');
});
目前的输出是:
1 undefined undefined 0 true 0 2 undefined undefined 0 false 0
undefined.. 5
对象的tasks
属性value
是一个对象数组,但您的代码假定它是一个对象.您的代码失败,因为tasks
数组没有dependency
属性,随后value.tasks.dependency.length
作为value.tasks.dependency
返回失败undefined
并且undefined
没有length
属性.
您应该从tasks
数组中获取特定元素:
.append('' + ' ');'+ (i+1) +' '+ ''+ value.tasks[0].name +' '+ ''+value.tasks[0].free_time+' '+ ''+value.tasks[0].done_ratio+' '+ ''+value.tasks[0].criticalTask+' '+ ''+value.tasks[0].dependency.length+' '+ '
或者遍历它:
var $target = $('.taskdetail'); $.each(mock_data_allocation.allocation, function(i, value) { $.each(value.tasks, function(ii, task) { $target.append('' + ' '); }); });'+ (ii+1) +' '+ ''+ task.name +' '+ ''+task.free_time+' '+ ''+task.done_ratio+' '+ ''+task.criticalTask+' '+ ''+task.dependency.length+' '+ '
此外,您不应该通过将常规对象传递给jQuery来创建jQuery对象.请改用$.each
效用函数.
对象的tasks
属性value
是一个对象数组,但您的代码假定它是一个对象.您的代码失败,因为tasks
数组没有dependency
属性,随后value.tasks.dependency.length
作为value.tasks.dependency
返回失败undefined
并且undefined
没有length
属性.
您应该从tasks
数组中获取特定元素:
.append('' + ' ');'+ (i+1) +' '+ ''+ value.tasks[0].name +' '+ ''+value.tasks[0].free_time+' '+ ''+value.tasks[0].done_ratio+' '+ ''+value.tasks[0].criticalTask+' '+ ''+value.tasks[0].dependency.length+' '+ '
或者遍历它:
var $target = $('.taskdetail'); $.each(mock_data_allocation.allocation, function(i, value) { $.each(value.tasks, function(ii, task) { $target.append('' + ' '); }); });'+ (ii+1) +' '+ ''+ task.name +' '+ ''+task.free_time+' '+ ''+task.done_ratio+' '+ ''+task.criticalTask+' '+ ''+task.dependency.length+' '+ '
此外,您不应该通过将常规对象传递给jQuery来创建jQuery对象.请改用$.each
效用函数.