2017-02-02 70 views
0

嗨,我有一個這樣的對象,我想複製其所有的父對象複製,如何所有的父對象從對象的數組使用lodash或JavaScript

permisions= [ 
{ 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":01 
     "permission": "NO", 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":02 
     "permission": "NO" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":03 
     "permission": "Yes" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":04 
     "permission": "Yes" 

    }, 
    { 
     "parent_id": "DISTRIBUTOR1", 
     "id": "DISTRIBUTOR2", 
     "city":0111    
     "permission": "NO" 
    }, 
    { 
     "parent_id": "DISTRIBUTOR1", 
     "id": "DISTRIBUTOR2", 
     "city":0112     
     "permission": "Yes" 
    }, 


    { 
     "parent_id": "DISTRIBUTOR2", 
     "id": "DISTRIBUTOR3", 
     "city":0333     
     "permission": "Yes" 
    }, 
    { 
     "parent_id": "DISTRIBUTOR2", 
     "id": "DISTRIBUTOR3", 
     "city":01111     
     "permission": "Yes" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR4", 
     "city":0444     
     "permission": "Yes" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR5", 
     "city":0555    
     "permission": "Yes" 
    } 
] 

如果我選擇ID :Distubutor3它包含PARENT_ID:「DISTRIBUTOR2」DISTRIBUTOR2包含父_id:DISTRIBUTOR1所以它具有複製,直到結束所有父對象,可以anybdy幫助我在此。輸出應該是這樣的,

result=[ { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":01 
     "permission": "NO", 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":02 
     "permission": "NO" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":03 
     "permission": "Yes" 
    }, 
    { 
     "parent_id": "", 
     "id": "DISTRIBUTOR1", 
     "city":04 
     "permission": "Yes" 

    }, 
    { 
     "parent_id": "DISTRIBUTOR1", 
     "id": "DISTRIBUTOR2", 
     "city":0111    
     "permission": "NO" 
    }, 
    { 
     "parent_id": "DISTRIBUTOR1", 
     "id": "DISTRIBUTOR2", 
     "city":0112     
     "permission": "Yes" 
    }, 

    { 
     "parent_id": "DISTRIBUTOR2", 
     "id": "DISTRIBUTOR3", 
     "city":0333    
     "permission": "Yes" 
    }, 
     { 
     "parent_id": "DISTRIBUTOR2", 
     "id": "DISTRIBUTOR3", 
     "city":01111     
     "permission": "Yes" 
    }] 
+0

是否要檢查該ID是否存在於數組中或只是以任何方式執行賦值。我的意思是,如果沒有'DISTRIBUTOR1'應該'DISTRIBUTOR2'獲得該屬性或不? –

+0

如果例如'DISTRIBUTOR2'不存在,應該'DISTRIBUTOR3'採用屬性'DISTRIBUTOR1'? –

+0

@ibrahimmahrir如果DISTRIBUTOR3不包含parent_id,它不應該複製任何內容, – Jeevan

回答

0

var permissions = [{"parent_id":"","id":"DISTRIBUTOR1","city":1,"permission":"NO"},{"parent_id":"","id":"DISTRIBUTOR1","city":2,"permission":"NO"},{"parent_id":"","id":"DISTRIBUTOR1","city":3,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR1","city":4,"permission":"Yes"},{"parent_id":"DISTRIBUTOR1","id":"DISTRIBUTOR2","city":73,"permission":"NO"},{"parent_id":"DISTRIBUTOR1","id":"DISTRIBUTOR2","city":74,"permission":"Yes"},{"parent_id":"DISTRIBUTOR2","id":"DISTRIBUTOR3","city":219,"permission":"Yes"},{"parent_id":"DISTRIBUTOR2","id":"DISTRIBUTOR3","city":585,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR4","city":292,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR5","city":365,"permission":"Yes"}]; 
 

 
// get the ID number ("DISTRIBUTOR???" return "???") 
 
function idNum(id) { 
 
    return parseInt(id.substr(11)); 
 
} 
 

 
function makeResult(perm, id){ 
 
    var num = idNum(id); 
 
    return perm.map(function(p){ 
 
    var n = idNum(p.id); 
 
    if(n < num && n != 1) // if the current object's ID is 1 < ID < max 
 
     p.parent_id = p.parent_id || ("DISTRIBUTOR" + (n-1)); // fill id if it's not already set 
 
    return p; 
 
    }); 
 
} 
 

 
var res = makeResult(permissions, "DISTRIBUTOR5"); // set all objects that have ID (1 < ID < 5) 
 

 
console.log(res)

注:例如,如果你打電話makeResult(permissions, "DISTRIBUTOR5");並沒有對象數組中有"DISTRIBUTOR2",那麼對象"DISTRIBUTOR3"會無論如何,他們的parent_id設置爲"DISTRIBUTOR2"

+0

在DISTRIBUTOR5的情況下,它不包含parent_id,因此它只會返回其ID爲的對象:DISTRIBUTOR5 – Jeevan

0

編輯:我想你的意思是說,只有那些父母在場的元素以及元素本身應該包含在新的輸出數組中。

如果是的話,試試這個:

var filterParents = function(obj){ 

    var mappings = {}; 

    permisions.forEach(function(perm){ 

     if(!mappings[perm.id]) 
      mappings[perm.id] = []; 

     mappings[perm.id].push(perm); 
    }); 

    var output = []; 
    output.push(obj); 


    while(obj.parent_id && obj.parent_id != '') 
    { 
     output = output.concat(mappings[ obj.parent_id ]); 
     obj = mappings[ obj.parent_id ] [ 0 ]; 
    } 

    return output; 

}; 

這是假設父母的ID的每一個元素都會有相同的祖父母ID。

相關問題