2017-04-15 213 views
2

我的代碼:如何包含數組對象轉換爲對象的對象

function convert(arr, parent) { 
    var out = []; 
    for(var i in arr) { 
     if(arr[i].parent == parent) { 
      var children = convert(arr, arr[i].id) 

      if(children.length) { 
       arr[i].children = children 
      } 
      out.push(arr[i]) 
     } 
    } 
    return out; //return Object.assign({}, out);tried this, but i lose parents childrens arrays 
}; 
arras = [ 
    {id: 1, name: "parent1", parent: null}, 
    {id: 2, name: "children1", parent: 1}, 
    {id: 3, name: "children2", parent: 1}, 
    {id: 4, name: "parent2", parent: null}, 
    {id: 5, name: "children3", parent: 4}, 
    {id: 6, name: "children4", parent: 4} 
] 
console.log(convert(arras, null)); 

如何最終的結果應該

{ 
    parent1: [ 
     {name: "children1"}, 
     {name: "children2"} 
    ], 
    parent2: [ 
     {name: "children3}, 
     {name: "children4"} 
    ] 
} 

我的輸出看起來是什麼至今:

[ 
    {id: 1, name: "parent1", parent: null}: [ 
     {id: 2, name: "children1", parent: 1}, 
     {id: 3, name: "children2", parent: 1}, 
    ], 
    {id: 4, name: "parent2", parent: null}: [ 
     {id: 5, name: "children3", parent: 4}, 
     {id: 6, name: "children4", parent: 4} 
    ] 
] 

所以首先,我必須做的是將主數組轉換爲對象,當我傾向於這樣做時,我失去了兩個父對象數組......還需要更改方式控制檯顯示對象,任何幫助表示讚賞。

回答

2

如果parent是根節點,您可以檢查樹。

var data = [{ id: 1, name: "parent1", parent: null }, { id: 2, name: "children1", parent: 1 }, { id: 3, name: "children2", parent: 1 }, { id: 4, name: "parent2", parent: null }, { id: 5, name: "children3", parent: 4 }, { id: 6, name: "children4", parent: 4 }], 
 
    tree = function (data, root) { 
 
     var r = {}, 
 
      o = {}; 
 

 
     data.forEach(function (a) { 
 
      if (a.parent === root) { 
 
       r[a.name] = []; 
 
       o[a.id] = r[a.name]; 
 
      } else { 
 
       o[a.parent] = o[a.parent] || []; 
 
       o[a.parent].push({ name: a.name }); 
 
      } 
 
     }); 
 
     return r; 
 
    }(data, null); 
 

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

2

試試這個。

function convert(arr) { 
 
    var parents = {}; 
 
    for (var i in arr) { 
 
    if (arr[i].parent === null) { 
 
     parents[arr[i].id] = arr[i].name 
 
    } 
 
    } 
 

 
    var out = {} 
 
    for (i in arr) { 
 
    if (arr[i].parent !== null) { 
 
     var parentName = parents[arr[i].parent]; 
 
     if (out.hasOwnProperty(parentName)) { 
 
     out[parentName].push(arr[i].name) 
 
     } else { 
 
     out[parentName] = [arr[i].name] 
 
     } 
 
    } 
 
    } 
 
    return out; 
 
}; 
 
arras = [{ 
 
    id: 1, 
 
    name: "parent1", 
 
    parent: null 
 
    }, 
 
    { 
 
    id: 2, 
 
    name: "children1", 
 
    parent: 1 
 
    }, 
 
    { 
 
    id: 3, 
 
    name: "children2", 
 
    parent: 1 
 
    }, 
 
    { 
 
    id: 4, 
 
    name: "parent2", 
 
    parent: null 
 
    }, 
 
    { 
 
    id: 5, 
 
    name: "children3", 
 
    parent: 4 
 
    }, 
 
    { 
 
    id: 6, 
 
    name: "children4", 
 
    parent: 4 
 
    } 
 
] 
 
//console.log(convert(arras, null)); 
 
alert(JSON.stringify(convert(arras)));

但對於多注意到它不能正常工作。如果你需要它,你必須保存的地圖對所有可能的父目錄

+0

功能那種無法處理這個沒有一個父ID子ID,分配所有無與倫比的孩子的名字到一個數組'arras.undefined'這是不是一個好名字屬性。此外,這些孩子的父母身份證在這個過程中會丟失,這將是一個重大失敗。 – Blindman67

+0

@ Blindman67如果輸入數據將包含沒有hape屬性「parent」的記錄,則您注意是正確的。但對我來說,它只是錯誤的輸入數據 –

2
arras.forEach(function(el){ 
    if(el.parent){ 
     el.parent=arras.find(e=>e.id==el.parent)||(console.error("no parent:"+el.parent),undefined); 
    } 
}); 
//resolved parent/childs.... 
var newmodel = arras.reduce(function(obj,el){ 
    if(el.parent){ 
    //child 
    obj[el.parent.name]=obj[el.parent.name]||[];//create new parent if neccessary 
    obj[el.parent.name].push({name:el.name}); 
    }else{ 
    //parent 
    obj[el.name]=obj[el.name]||[]; 
    } 
    return obj; 
    },{}); 

http://jsbin.com/renicijufi/edit?console

+1

你有錯字'aras.reduce' –

+0

@PavloZhukov謝謝;) –

+0

如果一個id是零,你的函數將失敗 – Blindman67

1

另一種方式:

var arrays = [ 
 
    {id: 1, name: 'parent1', parent: null}, 
 
    {id: 2, name: 'children1', parent: 1}, 
 
    {id: 3, name: 'children2', parent: 1}, 
 
    {id: 4, name: 'parent2', parent: null}, 
 
    {id: 5, name: 'children3', parent: 4}, 
 
    {id: 6, name: 'children4', parent: 4} 
 
]; 
 

 
// First, reduce the input arrays to id based map 
 
// This step help easy to select any element by id. 
 
arrays = arrays.reduce(function (map, el) { 
 
    map[el.id] = el; 
 

 
    return map; 
 
}, {}); 
 

 
var result = Object.values(arrays).reduce(function (result, el) { 
 
    if (!el.parent) { 
 
     result[el.name] = []; 
 
    } else { 
 
     result[arrays[el.parent].name].push(el.name); 
 
    } 
 

 
    return result; 
 
}, {}); 
 

 
console.log(result);

+0

如果一個孩子的id爲0,你的函數將失敗。搞砸了整個結果 – Blindman67

1

我覺得這符合您的要求

Obj = new Object(); 

for(i in arras){ 

    person = arras[i]; 

    if(person.parent != null){ 
     if(!Obj.hasOwnProperty(person.parent)){ 
      // here instead of the index you can use Obj["parent"+person.parent] get the exact thing. If you are using that use tha in rest of the code 
      Obj[person.parent] = new Array(); 
     } 

     Obj[person.parent].push(person);  
    } 
    else{ 
     if(!Obj.hasOwnProperty(person.id)){ 
      // Some parents might have kids not in the list. If you want to ignore, just remove from the else. 
      Obj[person.id] = new Array() 
     } 
    } 
} 

編輯:

Obj = new Object();

for(i in arras){ 

    person = arras[i]; 

    if(person.parent != null){ 
     if(!Obj.hasOwnProperty(person.parent)){ 
      // here instead of the index you can use Obj["parent"+person.parent] get the exact thing. If you are using that use tha in rest of the code 
      Obj[person.parent] = new Array(); 
     } 

     Obj[person.parent].push({name : person.name}); 
    } 
    else{ 
     if(!Obj.hasOwnProperty(person.id)){ 
      // Some parents might have kids not in the list. If you want to ignore, just remove from the else. 
      Obj[person.id] = new Array() 
     } 
    } 
} 

希望這會有所幫助。 :)

+0

你是唯一一個正確處理我扔掉它的所有測試用例,不會丟失數據,也不會錯誤地將孩子分組(如果沒有匹配的父母,也可能拋出),除了一個小點之外。結果不符合要求的結構。 'data.parentName:[{name:childName},...' – Blindman67

+0

檢查編輯。謝謝你的誇獎。快樂幫助你。 :) –

相關問題