Javascript:使用“for”遍历2级深度数组不产生另一个数组

发布于 2024-08-15 09:30:18 字数 1233 浏览 3 评论 0原文

抱歉,如果我的标题很难理解。让我解释一下。

使用我的结构示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-08-22 09:30:18

您已经说过问题是什么:“item”不包含数组...它包含一个等于子数组键的字符串。因此,您只需要使用该密钥:

var subarray;
for (var i in myArray) {
    subarray = myArray[i];
    for (var j in subarray) {
        ... // do stuff with subarray[j]
    }
}

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:

var subarray;
for (var i in myArray) {
    subarray = myArray[i];
    for (var j in subarray) {
        ... // do stuff with subarray[j]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文