在梦境中的梦境中的循环内循环

时间:2014-06-05 21:07:03

标签: javascript json nested-loops

我正在尝试使用一些API,作为回报,他们给了我一个巨大的JSON对象,我需要将它与我的JSON对象进行比较,但是,我有很多循环,它只是不再有趣了。

我当前的对象如下所示:

var courses = {
    art_117: {
        course_folder: true,
        sco_id: "12111",
        subFolders: [{
            folderName: "SET_01",
            isFolder: true,
            number: 1,
            sco_id: "121116"
        }, {
            folderName: 'SET_06',
            isFolder: true,
            number: 6,
            sco_id: '121117'
        }]
    }
}

返回的对象如下所示:

var json = {
    "status": {
        "code": "ok"
    },
    "scos": {
        "sco": [{
            "name": "SET_01",
            "url_path": "/134123/",
            "is_seminar": "false",
            "sco_id": "63453452",
            "folder_id": "12111",
            "type": "folder",
            "icon": "folder",
            "display_seq": "0",
            "duration": "",
            "is_folder": "1"
        }, {
            "name": "SET_02",
            "url_path": "/f4r8y3l0sy6/",
            "is_seminar": "false",
            "sco_id": "123413",
            "folder_id": "12111",
            "type": "folder",
            "icon": "folder",
            "display_seq": "0",
            "duration": "",
            "is_folder": "1"
        }, {
            "name": "SET_03",
            "url_path": "/f7d6lf2xbb1/",
            "is_seminar": "false",
            "sco_id": "12341234",
            "folder_id": "12111",
            "type": "folder",
            "icon": "folder",
            "display_seq": "0",
            "duration": "",
            "is_folder": "1"
        }]
    }
}

这是我当前的代码

if (typeof json.scos.sco != 'undefined') {
    var scos = json.scos.sco;
    var len = scos.length;
    for (var i = 0; i < len; i++) {
        var id = scos[i]['folder_id'];
        var name = scos[i]['name'];
        for (var j = 0; j < keys.length; j++) {
            if (adobe.courses[keys[j]]['sco_id'] == id) {
                var subFolders = adobe.courses[keys[j]]['subFolders'];
                for (var k = 0; k < sections.length; k++) {
                    if (sections[k]['folderName'] == name) {
                        courses[keys[j]]['subFolders'][k]['sco_id'] = scos[i]['sco_id'];
                        courses[keys[j]]['subFolders'][k]['isFolder'] = true;
                    }
                }
            }
        }
    }
}

如何重写以便没有3个嵌套循环?

0 个答案:

没有答案