[英]Access an array that's on a 3rd dimension with Javascript
I'm trying to get a sub array out of a larger array object. 我试图从更大的数组对象中获取一个子数组。
I am able to access the 2nd level using the code below: 我可以使用以下代码访问第二级:
var value = $(this).val();
var id = $(this).attr('id');
alert(id + "-" + value); // outputs OK the id and value veriables are good EG "I17-SATISFACTORY"
console.log(autoPop[id]); // gives me the array from I17
console.log(autoPop[id][value]); // says undefined
Here is the array: 这是数组:
{
"I34": [
{
"Y": [{
"I35": "N/A",
"I39": "Y",
"I43": "Y"
}],
"LIM": [{
"I35": "N/A",
"I36": "N/A"
}]
}
],
"I4": [
{
"0": [{
"I36": "N/A"
}],
"1": [{
"I7": "TEST OK ",
"I8": "N/A"
}]
}
],
"I17": [
{
"SATISFACTORY": [{
"I7": "test ok 2",
"I37": "N/A",
"I38": "N/A",
"I39": "Y",
"I43": "Y"
}],
"UNSATISFACTORY": [{
"I7": "TEST OK ",
"I8": "N/A"
}]
}
]
}
I've been trying with dot notation and brackets. 我一直在尝试使用点符号和方括号。 I found that you need to use brackets with variable
我发现您需要使用带有变量的方括号
Once i can access the array i then need to loop through the array and use jQuery to update the values of fields etc. 一旦我可以访问数组,则需要遍历数组并使用jQuery更新字段的值等。
Hopefully i'm on the right track and someone can work out what i'm doing wrong! 希望我走上正确的道路,有人可以解决我做错的事情!
Thanks Chris 谢谢克里斯
EDIT For clarification: The value i would like to get is, or an object array with: 编辑为澄清:我想获得的值是,或具有以下对象的数组:
[{
"I7": "test ok 2",
"I37": "N/A",
"I38": "N/A",
"I39": "Y",
"I43": "Y"
}]
Because there is an object in every level, you always need to first get that object, using [0]
, then, look for what you want: 因为每个级别都有一个对象,所以始终需要首先使用
[0]
获取该对象,然后查找所需的对象:
var arr = { "I34": [{ "Y": [{ "I35": "N/A", "I39": "Y", "I43": "Y" }], "LIM": [{ "I35": "N/A", "I36": "N/A" }] }], "I4": [{ "0": [{ "I36": "N/A" }], "1": [{ "I7": "TEST OK ", "I8": "N/A" }] }], "I17": [{ "SATISFACTORY": [{ "I7": "test ok 2", "I37": "N/A", "I38": "N/A", "I39": "Y", "I43": "Y" }], "UNSATISFACTORY": [{ "I7": "TEST OK ", "I8": "N/A" }] }] } console.log(arr["I17"]); console.log(arr["I17"][0]["SATISFACTORY"]); console.log(arr["I17"][0]["SATISFACTORY"][0]["I7"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.