4

サーバーからデータのリストを取得し、それを変換する必要があります。これの一部は、それを 3 次元配列に変換することです。「myArr[i].children.push(temp);」の後 配列のルートにプッシュされたオブジェクトのコピーを残します。コピーせずにプッシュできますか、またはこれらを削除するにはどうすればよいですか? (アンダースコア js が含まれています。優れた配列関数があることはわかっています:))

for (var i = 0; i < myArr.length; i++) {
    myArr[i].children = [];
    for (var q = 0; q < myArr.length; q++) {
        if (myArr[i].id == myArr[q].parentid) {
            var temp = {
                id: myArr[q].id,
                index: myArr[q].index,
                text: myArr[q].text
            }
            myArr[i].children.push(temp);
        };
    };  
};

データ

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": "0"
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": "1"
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": "2"
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": "0"
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": "1"
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation\/Soundpoint",
    "index": "2"
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": "1"
}, {
    "id": "15",
    "parentid": "0",
    "text": "Post-Sales Implementation Check List",
    "index": "7"
}, {
    "id": "16",
    "parentid": "15",
    "text": "Porting and New Number Details",
    "index": "0"
}, {
    "id": "18",
    "parentid": "15",
    "text": "Partner Setup",
    "index": "1"
}, {
    "id": "19",
    "parentid": "15",
    "text": "test",
    "index": "2"
}, {
    "id": "20",
    "parentid": "0",
    "text": "test",
    "index": "11"
}, {
    "id": "21",
    "parentid": "15",
    "text": "test",
    "index": "3"
}, {
    "id": "23",
    "parentid": "5",
    "text": "New Polycom",
    "index": "0"
}, {
    "id": "24",
    "parentid": "0",
    "text": "Test Markup",
    "index": "14"
}, {
    "id": "25",
    "parentid": "0",
    "text": "test",
    "index": "15"
}]

フォーマット後:

{
    "children": [{
        "id": "5",
        "parentid": "0",
        "text": "Device Guides",
        "index": "1",
        "children": [{
            "id": "10",
            "index": "0",
            "text": "Grandstream GXP-21XX"
        }, {
            "id": "11",
            "index": "1",
            "text": "Polycom Soundstation/Soundpoint"
        }, {
            "id": "23",
            "index": "2",
            "text": "New Polycom"
        }]
    }, {
        "id": "6",
        "parentid": "0",
        "text": "Pre-Sales Evaluation",
        "index": "0",
        "children": []
    }, {
        "id": "7",
        "parentid": "0",
        "text": "Router Setup Guides",
        "index": "2",
        "children": [{
            "id": "9",
            "index": "0",
            "text": "Sonicwall"
        }, {
            "id": "12",
            "index": "1",
            "text": "Cisco"
        }]
    }, {
        "id": "9",
        "parentid": "7",
        "text": "Sonicwall",
        "index": "0",
        "children": []
    }, {
        "id": "10",
        "parentid": "5",
        "text": "Grandstream GXP-21XX",
        "index": "0",
        "children": []
    }, {
        "id": "11",
        "parentid": "5",
        "text": "Polycom Soundstation/Soundpoint",
        "index": "1",
        "children": []
    }, {
        "id": "12",
        "parentid": "7",
        "text": "Cisco",
        "index": "1",
        "children": []
    }, {
        "id": "15",
        "parentid": "0",
        "text": "Post-Sales Implementation Check List",
        "index": "7",
        "children": [{
            "id": "16",
            "index": "0",
            "text": "Porting and New Number Details"
        }, {
            "id": "18",
            "index": "1",
            "text": "Partner Setup"
        }, {
            "id": "19",
            "index": "2",
            "text": "test"
        }, {
            "id": "21",
            "index": "3",
            "text": "test"
        }]
    }, {
        "id": "16",
        "parentid": "15",
        "text": "Porting and New Number Details",
        "index": "0",
        "children": []
    }, {
        "id": "18",
        "parentid": "15",
        "text": "Partner Setup",
        "index": "1",
        "children": []
    }, {
        "id": "19",
        "parentid": "15",
        "text": "test",
        "index": "2",
        "children": []
    }, {
        "id": "20",
        "parentid": "0",
        "text": "test",
        "index": "11",
        "children": []
    }, {
        "id": "21",
        "parentid": "15",
        "text": "test",
        "index": "3",
        "children": []
    }, {
        "id": "23",
        "parentid": "5",
        "text": "New Polycom",
        "index": "2",
        "children": []
    }, {
        "id": "24",
        "parentid": "0",
        "text": "Test Markup",
        "index": "14",
        "children": []
    }, {
        "id": "25",
        "parentid": "0",
        "text": "test",
        "index": "15",
        "children": []
    }]
}
4

2 に答える 2

2

コメントでの友好的な議論に続いて:

var zeroObj = {"children":[]};
for (var i = 0; i < myArr.length; i++) {
    if(myArr[i].parentid === 0) {
        zeroObj.children.push(myArr[i]);
    } else {
        for (var q = 0; q < myArr.length; q++) {
            if (myArr[i].parentid == myArr[q].id) {
                myArr[q].children = myArr[q].children || [];
                myArr[q].children.push(myArr[i]);
            };
        }; 
    }
};
于 2013-04-16T14:23:39.447 に答える