[英]How to access values in a JavaScript Object
This question has been asked before, however, the current answers don't solve my problem.之前已经问过这个问题,但是,当前的答案并不能解决我的问题。 When console.log-ing the value I am trying to grab from my object (an object retrieved from JSON) I get back some sort of当控制台记录我试图从我的 object (从 JSON 检索到的 object)获取的值时,我得到了某种
"Uncaught TypeError: Cannot read property 'approximateGroup' of undefined" “未捕获的 TypeError:无法读取未定义的属性 'approximateGroup'”
the JSON data is assigned to a var named rxcui and consists of the following: JSON 数据分配给一个名为 rxcui 的变量,包括以下内容:
{"approximateGroup":{"inputTerm":"paxil","maxEntries":"1","option":"0","comment":"","candidate":
[{"rxcui":"114228","rxaui":"826081","score":"100","rank":"1"},
{"rxcui":"114228","rxaui":"826082","score":"100","rank":"1"},
{"rxcui":"114228","rxaui":"8053434","score":"100","rank":"1"}]}}
and my code is我的代码是
console.log(rxcui.[0].approximateGroup.candidate[0].rxcui);
I have tried inspecting the object in my browser, copying the path and using every combination I could possibly think to for the last few hours.我尝试在浏览器中检查 object,复制路径并使用过去几个小时我可能想到的所有组合。 I understand that if I get a return of undefined then my path is wrong, but this is what path makes sense.我知道如果我得到未定义的返回,那么我的路径是错误的,但这是有意义的路径。 The exact path my browser tells me its at is:我的浏览器告诉我它的确切路径是:
[0].approximateGroup.candidate[0].rxcui
I've tried finding the path in the success function jquery for the JSON page as well with no success.我已经尝试在 JSON 页面的成功 function jquery 中找到路径,但没有成功。
Please help, I'm going insane:+)请帮忙,我要疯了:+)
Your first property is the object approximateGroup , then you can acess the candidate array indicating the first index, then you acess the rxcui property of this object.您的第一个属性是 object近似组,然后您可以访问指示第一个索引的候选数组,然后您访问此 object 的 rxcui 属性。
console.log(YourOBJ.approximateGroup.candidate[0].rxcui)
Here's your json in a more readable format这是您的 json,格式更易读
{
"approximateGroup": {
"inputTerm": "paxil",
"maxEntries": "1",
"option": "0",
"comment": "",
"candidate": [
{
"rxcui": "114228",
"rxaui": "826081",
"score": "100",
"rank": "1"
},
{
"rxcui": "114228",
"rxaui": "826082",
"score": "100",
"rank": "1"
},
{
"rxcui": "114228",
"rxaui": "8053434",
"score": "100",
"rank": "1"
}
]
}
}
rxcui
is a property of an object within the candidate
array. rxcui
是candidate
数组中 object 的属性。 The candidate
array is a property within the approximategroup
object. candidate
数组是approximategroup
组 object 中的一个属性。
If we assign the json to a variable x
then you can address it in this way如果我们将 json 分配给变量x
那么您可以通过这种方式对其进行寻址
const firstRxcuit = x.approximateGroup.candidate[0].rxcui
I think you are mixing up array and object notation, you cant use [Int] by index in an object, only an array, you can however use ["some prop"] against an object to find a value of a property我认为您正在混淆数组和 object 表示法,您不能在 object 中按索引使用 [Int],只能是一个数组,但是您可以针对 object 使用 ["some prop"] 来查找 a 值
var rxcui = {"approximateGroup":{"inputTerm":"paxil","maxEntries":"1","option":"0","comment":"","candidate": [{"rxcui":"114228","rxaui":"826081","score":"100","rank":"1"}, {"rxcui":"114228","rxaui":"826082","score":"100","rank":"1"}, {"rxcui":"114228","rxaui":"8053434","score":"100","rank":"1"}]}} console.log(rxcui.approximateGroup.candidate[0].rxaui) // returns "826081"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.