使用jQuery / JS通过嵌套JSON在JSON / Iterate中获取子项内的子项? [英] Get child inside a child in JSON / Iterate through nested JSON with jQuery/JS?

查看:107
本文介绍了使用jQuery / JS通过嵌套JSON在JSON / Iterate中获取子项内的子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可行,如果是这样的话,
如何循环使用:

I wonder if this is possible, if so how can I loop through this:

[
{
    "name": "Hello",
    "views": 10,
    "subMovie": [
        {
        "name": "World",
        "views": 10,
        "subMovie": [
            {
            "name": "Test 1",
            "views": 10,
            "subMovie": [
                {
                "name": "Test 2",
                "views": 10,
                "subMovie": [
                    {
                        "name": "Test 3",
                        "views": 10,
                        "subMovie": [],
                        "id": 5
                    }
                ],
                "id": 4
                }
            ],
            "id": 3
            }
        ],
        "id": 2
        }
    ],
    "id": 1
}
]

到达最后一个孩子(subMovie)?

to get to the last child(subMovie)?

我想全部访问它们并打印出来,
但是如何使用jQuery / JavaScript在子项中循环这个子项?

I want to access them all and print them out, but how do I loop through this child inside a child with jQuery/JavaScript?

推荐答案

以下代码将启动使用 movies 数组的最后一个元素。之后,它将通过选择它访问的每部电影的最后一个数组元素进入 subMovie 树,直到没有 subMovie s left。

The following code will start with the last element of the movies array. After that it will descend into the subMovie tree by selecting the last array element of every movie it visits until there are no subMovies left.

var movies = [
{
    "name": "Hello",
    "views": 10,
    "subMovie": [
        ...
    ],
    "id": 1
}
];

var m = movies[movies.length - 1];

while(m.subMovie.length >= 1) {
    console.log(m.name);
    m = m.subMovie[m.subMovie.length - 1];   
}

console.log(m); //Object {name: "Test 3", views: 10, subMovie: Array[0], id: 5}

这篇关于使用jQuery / JS通过嵌套JSON在JSON / Iterate中获取子项内的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆