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

如何从JavaScript对象中提取数据

如何解决《如何从JavaScript对象中提取数据》经验,为你挑选了1个好方法。

有一个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效用函数.



1> undefined..:

对象的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效用函数.

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