2017-09-14 111 views
1

我有一個JSON數組。我嘗試轉換嵌套分層樹。但它沒有工作。我需要一個使用這個JSON的分層樹。Json使用分層樹使用javascript

這裏是我的數據:

[{ 
    "_id" : "59b65ee33af7a11a3e3486c2", 
    "C_TITLE" : "Sweet and Snacks", 
    "C_PARENT" : "0", 
    "C_ICON" : "", 
    "C_IMAGE" : "59b65ee33af7a11a3e3486c2_sweets.jpg", 
    "C_STATUS" : "Active" 
} 
{ 
    "_id" : "59b663d709da571dc3d79f49", 
    "C_TITLE" : "Groceries", 
    "C_PARENT" : "0", 
    "C_ICON" : "", 
    "C_IMAGE" : "59b663d709da571dc3d79f49_grocery.jpg", 
    "C_STATUS" : "Active" 
}, 
{ 
    "_id" : "59b6648209da571dc3d79f4a", 
    "C_TITLE" : "Dals & Pulses", 
    "C_PARENT" : "59b663d709da571dc3d79f49", 
    "C_ICON" : "", 
    "C_IMAGE" : "59b6648209da571dc3d79f4a_dals.jpg", 
    "C_STATUS" : "Active" 
}, 
{ 
    "_id" : "59b6657509da571dc3d79f4c", 
    "C_TITLE" : "Rice & Rice products", 
    "C_PARENT" : "59b663d709da571dc3d79f49", 
    "C_ICON" : "", 
    "C_IMAGE" : "59b6657509da571dc3d79f4c_rice.jpg", 
    "C_STATUS" : "Active" 
}] 

我需要這樣的輸出

[ 
    { 
     " _id" : "59b65ee33af7a11a3e3486c2", 
     "C_TITLE" : "Sweet and Snacks", 
     "C_PARENT" : "0", 
     "C_ICON" : "", 
     "C_IMAGE" : "59b65ee33af7a11a3e3486c2_sweets.jpg", 
     "C_STATUS" : "Active", 
     children:[] 


    } 
    ] 

基於Check C_PARENT和_id。

+0

可以添加您的預期輸出 – prasanth

+0

[ \t { \t \t 「_id」: 「59b65ee33af7a11a3e3486c2」, \t \t 「C_TITLE」: 「甜零食」, \t \t 「C_PARENT」:「0」, \t \t「C_ICON」:「」, \t \t 「C_IMAGE」: 「59b65ee33af7a11a3e3486c2_sweets.jpg」, \t \t 「C_STATUS」: 「活動」, \t \t孩子:{ \t \t 「_id」: 「ghhgfhfhhhg」, \t \t \t 「C_TITLE」:「甜零食 「 \t \t \t 」C_PARENT「: 」59b65ee33af7a11a3e3486c2「, \t \t \t 」C_ICON「: 」「, \t \t \t 」C_IMAGE「:」 59b65ee33af7a1 1a3e3486c2_sweets.jpg」, \t \t \t 「C_STATUS」: 「活躍」, \t \t} \t} ] – sarankani

+0

感謝烏爾reply.I需要上面的輸出。 – sarankani

回答

0

基本上,你需要,而不是

o[a.id] 

o[a._id] 

,因爲你的數據有_id作爲密鑰ID。

另一個問題是與root的嚴格比較,那裏你需要一個字符串作爲值'0',因爲那是你的價值。

var data = [{ _id: "59b65ee33af7a11a3e3486c2", C_TITLE: "Sweet and Snacks", C_PARENT: "0", C_ICON: "", C_IMAGE: "59b65ee33af7a11a3e3486c2_sweets.jpg", C_STATUS: "Active" }, { _id: "59b663d709da571dc3d79f49", C_TITLE: "Groceries", C_PARENT: "0", C_ICON: "", C_IMAGE: "59b663d709da571dc3d79f49_grocery.jpg", C_STATUS: "Active" }, { _id: "59b6648209da571dc3d79f4a", C_TITLE: "Dals & Pulses", C_PARENT: "59b663d709da571dc3d79f49", C_ICON: "", C_IMAGE: "59b6648209da571dc3d79f4a_dals.jpg", C_STATUS: "Active" }, { _id: "59b6657509da571dc3d79f4c", C_TITLE: "Rice & Rice products", C_PARENT: "59b663d709da571dc3d79f49", C_ICON: "", C_IMAGE: "59b6657509da571dc3d79f4c_rice.jpg", C_STATUS: "Active" }], 
 
    result = function (array, root) { 
 
     var r = [], o = {}; 
 
     array.forEach(function (a) { 
 
      a.children = o[a._id] && o[a._id].children; // change to a._id 
 
      o[a._id] = a;        // change to a._id 
 
      if (a.C_PARENT === root) {     // strict comparison! 
 
       r.push(a); 
 
       return; 
 
      } 
 
      o[a.C_PARENT] = o[a.C_PARENT] || {}; 
 
      o[a.C_PARENT].children = o[a.C_PARENT].children || []; 
 
      o[a.C_PARENT].children.push(a); 
 
     }); 
 
     return r; 
 
    }(data, "0");          // "0" as root 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

我懷疑這是否適用於未排序的多級樹。 – bluehipy

+0

@bluehipy,它正在處理任何已排序的原始數據,因爲對象保留了所有對節點的引用。 –

+0

@bluehipy,你可以看看[這裏](https://stackoverflow.com/a/37806854/1447675),該功能是如何工作的。 –