我有以下對象。我想要做的是提出一個函數,該函數將指定給定對象是否是id的父代的後代。例如,我想這樣做:lodash歡迎。如何檢查嵌套對象是不使用jQuery的另一個對象的後代
isThisDescendant(7,1) //true
isThisDescendant(1,7) //false
isThisDescendant(7,3) //false
isThisDescendant(3,2) //true
object = {
children : [
{
name: 'a',
id: 1,
children: [
{
name: 'b',
id: 2,
children: [
{
name: 'c',
id: 3,
children: []
},
{
name: 'cc',
id: 4,
children: []
}
]
},
{
name: 'ba',
id: 6,
children: [
{
name: 'c',
id: 8,
children: []
},
{
name: 'cd',
id: 7,
children: []
}
]
}
]
},
{
name: 'bb',
id: 10,
children: []
}
]
}
'id'應該是一個獨特的價值,但是從我看到它不是(代碼味道!)。有'id'' 3'的兩個實例。你如何處理這種情況? –
@KingKongFrog你試過了什麼?您應該提供您嘗試過的和沒有工作的代碼,而不是要求解決方案。 – SimplyComplexable