Javascript:使用“for”遍历2级深度数组不产生另一个数组
抱歉,如果我的标题很难理解。让我解释一下。
使用我的结构示例:
Array
(
[2] => Array
(
[0] => stdClass Object
(
[category_id] => 2
[category_name] => women
[project_id] => 1
[project_name] => Balloons
)
)
[1] => Array
(
[0] => stdClass Object
(
[category_id] => 1
[category_name] => men
[project_id] => 2
[project_name] => Cars
)
[1] => stdClass Object
(
[category_id] => 1
[category_name] => men
[project_id] => 3
[project_name] => Houses
)
)
然后,一旦我有了它,我就将其发送出去,由 javascript 进行评估(这是成功的)。 Console.log 实际上显示我的 eval'd json 实际上现在是一个对象。
现在,如果我 console.log(myArray[2]),它会将其显示为包含另一个数组的数组。这也是正确的
,但是!..如果我尝试这样做:
for (item in myArray[2]) {
...
}
或这样:
newVar = myArray[2]
for (item in newVar) {
...
}
“item”不包含应有的数组。它包含一个等于子数组键的字符串。在这种情况下是“0”,
我在这里错过了什么? :(
感谢您的帮助!
Sorry if my title is hard to understand. Let me explain.
To use this example of my structure:
Array
(
[2] => Array
(
[0] => stdClass Object
(
[category_id] => 2
[category_name] => women
[project_id] => 1
[project_name] => Balloons
)
)
[1] => Array
(
[0] => stdClass Object
(
[category_id] => 1
[category_name] => men
[project_id] => 2
[project_name] => Cars
)
[1] => stdClass Object
(
[category_id] => 1
[category_name] => men
[project_id] => 3
[project_name] => Houses
)
)
Then once i have that, i send it out to be eval'd by javascript(which is successful). Console.log does in fact shows that's my eval'd json is in fact now an object.
Now, If i console.log(myArray[2]), it will show it as an array that contains another array. Which is also correct
BUT!.. if i try to do this:
for (item in myArray[2]) {
...
}
or this:
newVar = myArray[2]
for (item in newVar) {
...
}
"item" doesn't contain the array as it should. it contains a string equal the sub arrays' key. Which in this case is "0"
What am I missing here guys? :(
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经说过问题是什么:“item”不包含数组...它包含一个等于子数组键的字符串。因此,您只需要使用该密钥:
You already said what the problem was: "item" doesn't contain the array... it contains a string equal the sub arrays' key. So, you just need to use that key: