为许多子节点运行每个函数的正确方法是什么?

时间:2014-11-21 21:23:17

标签: javascript jquery arrays loops each

我有一个数组和许多对象。我希望能够在每个files/a节点中运行每个函数来更改每个files/and subpage > file。如何使用正确的每个或循环函数执行此操作?我想到了这一点,这是我脑海中的结构,看起来很糟糕。

$.each(main, function( index, value ) {
    $.each(index.subpage, function( index, value ) {
        $.each(index.files, function( index, value ) {
             value.replace("files/a", "files/and");
        });
    });
});

主要对象如下所示。

{
    "main": [
        {
            "title": "AAA",
            "hash": "a",
            "subpage": [
                {
                    "title": "b",
                    "hash": "b",
                    "subpage": [
                        {
                            "title": "c",
                            "hash": "c",
                            "subpage": [],
                            "files": [
                                {
                                    "files/a/b/c/01_clandestino_dev%20%282%29.jpg": {}
                                },
                                {
                                    "files/a/b/c/01_clandestino_dev%20%283%29.jpg": {}
                                }
                            ],
                            "content": "",
                            "layout": "standart"
                        }
                    ],
                    "files": [
                        {
                            "files/a/b/01_clandestino_dev%20%282%29.jpg": {}
                        },
                        {
                            "files/a/b/01_clandestino_dev%20%283%29.jpg": {}
                        }
                    ],
                    "content": "asd123",
                    "layout": "standart"
                }
            ],
            "files": [
                {
                    "files/a/01_clandestino_dev.jpg": {}
                },
                {
                    "files/a/01.Creative_Collective_Effect_Overview.jpg": {}
                },
                {
                    "files/a/01.Bor%C3%A5s_H%C3%B6gskola_Website_Narrow.jpg": {}
                }
            ],
            "content": "AAAb",
            "layout": "standart",
            "menuItem": "true"
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

递归是唯一的解决方案。您需要编写一个处理"页面":

的函数
  • 使用递归处理当前页面内的每个子页面
  • 处理当前页面内的每个文件

每个文件都是一个带有一个键的对象;您需要添加新密钥并使用delete运算符删除旧密钥。



var o = {
    "main": [{
        "title": "AAA",
        "hash": "a",
        "subpage": [{
            "title": "b",
            "hash": "b",
            "subpage": [{
                "title": "c",
                "hash": "c",
                "subpage": [],
                "files": [{
                    "files/a/b/c/01_clandestino_dev%20%282%29.jpg": {}
                }, {
                    "files/a/b/c/01_clandestino_dev%20%283%29.jpg": {}
                }],
                "content": "",
                "layout": "standart"
            }],
            "files": [{
                "files/a/b/01_clandestino_dev%20%282%29.jpg": {}
            }, {
                "files/a/b/01_clandestino_dev%20%283%29.jpg": {}
            }],
            "content": "asd123",
            "layout": "standart"
        }],
        "files": [{
            "files/a/01_clandestino_dev.jpg": {}
        }, {
            "files/a/01.Creative_Collective_Effect_Overview.jpg": {}
        }, {
            "files/a/01.Bor%C3%A5s_H%C3%B6gskola_Website_Narrow.jpg": {}
        }],
        "content": "AAAb",
        "layout": "standart",
        "menuItem": "true"
    }]
};

function process_page(page) {
    if (page.main || page.subpage) {
        $.each(page.main || page.subpage, function(i, subpage) {
            process_page(subpage);
        });
    }
    if (page.files) {
        $.each(page.files, function(i, file) {
            $.each(file, function(oldname, value) {
                var newname = oldname.replace("files/a", "files/and");
                console.log("old: " + oldname);
                console.log("new: " + newname);
                file[newname] = value;
                delete file[oldname];
            });
        });
    }
}
process_page(o);
console.log(o);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
&#13;
&#13;
&#13;