2012-03-19 121 views
0

我无法访问本身是大对象属性的对象数组。当数组本身是对象的属性时,无法访问数组对象

console.log(skillprofiles); 
console.log(skillprofiles.skills); 

上述语句返回两个对象,每个具有是一个属性的阵列的,看似,对象的有效阵列(在此示例“技能”)。我不能访问“技能”数组属性。我最终试图获取的数据是“item_name”属性。据我所知,应该通过可访问:

skillprofiles[0].skills[0].item_name 

有趣的是,验证“技能”属性的类型与代替阵列“的typeof”返回“对象”。这可能是因为我正在将代码中的一个对象数组从一个对象传递到另一个对象。我认为我正确地做了这件事,并且似乎已经通过我的日志记录证实了这一点。

更新与实际数据:

[{ 
"skillprofile_id": 144, 
"skillprofile_name": " On-boarding", 
"start_date": "January 30 2012", 
"progress": 0, 
"complete_date": " ", 
"category": 1053, 
"skills": [{ 
    "category": "Acquire: Onboard New Hires", 
    "item_name": "3. On-boarding a New Hire - Day 1", 
    "skill_type": "activity", 
    "object_id": 68, 
    "duration": "8:00:00", 
    "sco_status_code": 6, 
    "action": "activity", 
    "has_forums": false 
}, { 
    "category": "Acquire: Onboard New Hires", 
    "item_name": "4. On-boarding Checklist", 
    "skill_type": "activity", 
    "object_id": 67, 
    "duration": "1:00:00", 
    "sco_status_code": 6, 
    "action": "activity", 
    "has_forums": false 
}] 
}, { 
"skillprofile_id": 143, 
"skillprofile_name": " Setting Up Systems", 
"start_date": "January 30 2012", 
"progress": 0, 
"complete_date": " ", 
"category": 1053, 
"skills": [{ 
    "category": "Acquire: Onboard New Hires", 
    "item_name": "1. Office Organization", 
    "skill_type": "activity", 
    "object_id": 65, 
    "duration": "4:00:00", 
    "sco_status_code": 6, 
    "action": "activity", 
    "has_forums": false 
}, { 
    "category": "Acquire: Onboard New Hires", 
    "item_name": "2. Welcome to the Team Documents", 
    "skill_type": "activity", 
    "object_id": 66, 
    "duration": "2:00:00", 
    "sco_status_code": 6, 
    "action": "activity", 
    "has_forums": false 
}, { 
    "category": "Acquire: Onboard New Hires", 
    "item_name": "3. Welcome Documents: Feedback ", 
    "skill_type": "activity", 
    "object_id": 150, 
    "duration": "1:00:00", 
    "sco_status_code": 6, 
    "action": "activity", 
    "has_forums": false 
}] 
}] 

更新2:

鉴于答复迄今,看来我应该能够运行以下命令:

$.each(skillprofiles, function(){ 
    console.log(this.skills) 
}); 

我可以从控制台执行此操作,但是从我的代码运行时,控制台语句返回为“未定义”

+0

您是否试过skillprofiles.skills [0] .item_name或skillprofiles.skills.item_name? – 2012-03-19 17:28:38

+0

我认为你要么向我们展示真实/工作代码,要么你必须将代码包含在你建立'skillprofiles'和'skills'对象的地方。就目前而言,我们没有关于这些对象真正的信息。 FYI:'typeof [1,2,3] ==“object”' - 这对阵列来说是正常的。 – jfriend00 2012-03-19 17:28:46

+0

您需要发布数据的实​​际外观。从你在这里发布的信息来看,这是不可能的。 – Pointy 2012-03-19 17:29:19

回答

1

在你的第一块代码中,技能直接是技能形态的一个孩子。而在第二个,你写skillprofiles [o]。技能。如果第一个作品,也许你的代码应该是:

skillprofiles.skills[0].item_name 
0

随着该数据结构,适当的访问将是:

skillprofiles[i].skills[j].item_name 

你可以看到它在这里工作:http://jsfiddle.net/jfriend00/wPLZZ/


skillprofiles是一个对象数组,因此您从数组中获取对象:

skillprofiles[i] 

该数组中的每个对象,都具有多个属性和那些属性之一被命名为skills,所以你会得到这个属性与

skillprofiles[i].skills 

skills属性的内容是另一个数组,所以你会得到从阿雷这个项目:

skillprofiles[i].skills[j] 

该数组拥有自己的属性的对象,所以你会访问这个特定属性:

skillprofiles[i].skills[j].item_name