将对象数组转换为父子关系

时间:2016-08-09 19:07:55

标签: javascript

我想将vm.scholasticlist转换为vm.parentList2。 在vm.scholasticlist中, s_gid 是唯一键, parent_id 指向父 s_gid 即可。

vm.scholasticlist是我从WebService获得的返回。 我正在尝试关注javascript代码,但无法构建它。

vm.scholasticlist = [];
vm.parentList = [];
vm.parentList2 = [];

for (var i in vm.scholasticlist) {
    if(vm.scholasticlist[i]['parent_id'] == 0 ){
        vm.parentList.push(vm.scholasticlist[i])
    }
}


vm.parentList2 = vm.parentList;

for (var i in vm.parentList) {

    console.log(vm.parentList[i]['s_gid'])
    for (var x in vm.scholasticlist) {
        if(vm.scholasticlist[x]['parent_id'] === vm.parentList[i]['s_gid'] ){
            console.log(vm.parentList[i]['s_gid'])
            vm.parentList2.push(vm.scholasticlist[x])
        }
    }
}

vm.scholasticlist = [{
    "s_gid": "10",
    "m_s_p_id": "1",
    "subject_group_name": "Foundation of Information Technology",
    "parent_id": "0",
    "sname": ""
}, {
    "s_gid": "11",
    "m_s_p_id": "2",
    "subject_group_name": "Life Skills",
    "parent_id": "0",
    "sname": ""
}, {
    "s_gid": "15",
    "m_s_p_id": "2",
    "subject_group_name": "Thinking Skills",
    "parent_id": "11",
    "sname": "Th.sk"
}, {
    "s_gid": "15",
    "m_s_p_id": "2",
    "subject_group_name": "Thinking Skills",
    "parent_id": "11",
    "sname": "Th.sk"
}]

想要使用父子关系构建新的JSON

vm.parentList2 = [{
    "s_gid": "10",
    "m_s_p_id": "1",
    "subject_group_name": "Foundation of Information Technology",
    "parent_id": "0",
    "sname": ""
}, {
    "s_gid": "11",
    "m_s_p_id": "2",
    "subject_group_name": "Life Skills",
    "parent_id": "0",
    "sname": "",
    "child": [{
        "s_gid": "15",
        "m_s_p_id": "2",
        "subject_group_name": "Thinking Skills",
        "parent_id": "11",
        "sname": "Th.sk"
    }, {
        "s_gid": "15",
        "m_s_p_id": "2",
        "subject_group_name": "Thinking Skills",
        "parent_id": "11",
        "sname": "Th.sk"
    }]
}]

2 个答案:

答案 0 :(得分:1)

您可以使用此功能 - 它使用ES6语法:



<select multiple ng-model="selectedCategories" ng-options="category for category in categories"></select>

<div class="job-listing" ng-bind="job | json"
     ng-repeat="job in jobs | getMatches:{searchField: 'Categories', searchTerms: selectedCategories}">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

scholasticlist = [{
    "s_gid": "10",
    "m_s_p_id": "1",
    "subject_group_name": "Foundation of Information Technology",
    "parent_id": "0",
    "sname": ""
}, {
    "s_gid": "11",
    "m_s_p_id": "2",
    "subject_group_name": "Life Skills",
    "parent_id": "0",
    "sname": ""
}, {
    "s_gid": "15",
    "m_s_p_id": "2",
    "subject_group_name": "Thinking Skills",
    "parent_id": "11",
    "sname": "Th.sk"
}, {
    "s_gid": "15",
    "m_s_p_id": "2",
    "subject_group_name": "Thinking Skills",
    "parent_id": "11",
    "sname": "Th.sk"
}];

// first convert your original array into a dictionary. That way we can do quick
// lookups by id
var dict = scholasticlist.reduce(function(p,c) {
    p[c.s_gid] = c;
    return p;
},{});

// then just go through your list again and push items into an array
var parentList2 = scholasticlist.reduce(function(p,c) {
    if (c.parent_id === "0") {     // has no parent
        p.push(c);
    } else {                       // has a parent, so we find it and then add to
                                   // the children
        if (!dict[c.parent_id].child) { // really ought to be children - it's an array!
            dict[c.parent_id].child = [];
        }
        dict[c.parent_id].child.push(c);
    }
    return p;
},[]);

console.log(JSON.stringify(parentList2, null, 2));