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

What is the proper way to run each function for many child nodes?

本文关键字:方法 是什么 子节点 运行 函数      更新时间:2023-09-26

我有一个数组和许多对象。我希望能够运行每个函数来更改每个files/a,并在每个subpage > file节点中使用files/and。如何使用适当的 each 或循环函数来执行此操作?我想到了这一点,这就是我脑海中的结构,看起来像是可怕的解决方案。

$.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"
        }
    ]
}

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

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

每个文件都是一个具有一个键的对象;您需要使用 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>