嗨,我有一個這樣的對象,我想複製其所有的父對象複製,如何所有的父對象從對象的數組使用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"
}]
是否要檢查該ID是否存在於數組中或只是以任何方式執行賦值。我的意思是,如果沒有'DISTRIBUTOR1'應該'DISTRIBUTOR2'獲得該屬性或不? –
如果例如'DISTRIBUTOR2'不存在,應該'DISTRIBUTOR3'採用屬性'DISTRIBUTOR1'? –
@ibrahimmahrir如果DISTRIBUTOR3不包含parent_id,它不應該複製任何內容, – Jeevan