fresher fresher - 4 months ago 9
Javascript Question

transforming object array into parent child relation

I am tring to convert

vm.scholasticlist
into
vm.parentList2
.
In
vm.scholasticlist
, s_gid is unique key, and parent_id point to parent s_gid.

vm.scholasticlist is what I am getting from WebService as return.
I am trying following javascript code, but unable to build it.

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


want to build new JSON with parent child relation

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

Answer

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));