2016-08-09 67 views
-2

我試圖將vm.scholasticlist轉換爲vm.parentList2。 在vm.scholasticlists_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

我真的沒有看到任何JSON,它看起來像javascript給我? – adeneo

+0

@ adeneo,是的,我正在嘗試基於第一個與父子關係與JAVASCRIPT構建新的JSON數組。第一個JSON數組是沒有父子的web服務的響應。 – fresher

+0

@fresher:這不是JSON,它只是Javascript對象。 JSON是一個*字符串*,你有什麼是對象文字。 JSON的談話完全與你的問題無關。 –

回答

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

+0

是否有可能在每個subject_group_name前加上一個像「 - 」這樣的符號,根據孩子的深度。例如第一個孩子「 - 」,第二級孩子「 - 」等 – fresher

+0

@fresher:當然,但這是一個完全不同的問題。 –

+0

是的。它是。我試圖操作你的代碼,但它只爲所有的孩子提供單一的「 - 」。你能幫忙嗎? – fresher

1

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

function buildTree(list) { 
 
    // create a hash for the array. 
 
    // Start with a "root" entry for linking nodes with parentid = 0: 
 
    var hash = { '0': { child: [] } }; 
 
    list.forEach(obj => hash[obj.s_gid] = Object.assign(obj, { child: [] })); 
 
    
 
    // move items into the appropriate child array 
 
    list.forEach(obj => hash[obj.parent_id].child.push(obj)); 
 
    
 
    // return the child array in the root element: 
 
    return hash['0'].child; 
 
} 
 

 
// Sample data 
 
var vm = {}; 
 
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" 
 
}]; 
 

 
// Transform to tree 
 
vm.scholasticlist = buildTree(vm.scholasticlist); 
 

 
// Output result 
 
console.log(vm.scholasticlist);