menu:[{ id:'' name:'', child:'[{ id:'' name:'', child:'[]' }]' }]Este elemento secundario está anidado infinitamente. ¿Cómo recorrer este árbol para obtener datos de la identificación pasada del niño?
let obj = { menu:[{ id:1, name:'Raj', child:[{ id:2, name:'vivek', child:[{ id:3, name:'sameer', child:[{ id:4, name:'sahil', child:[] }] }] }] }] } let userInput = 2; function checkid(obj1){ let { id, name:cname, child } = obj1; if(id === userInput){ return obj1; } else{ return checkid(child[0]); } } console.log(checkid(obj.menu[0]));¡¡Espero que esto te ayude!!
function handleElement(el){ if('id' in el){ // do something with el['id'] } if('child' in el){ handleElement(el.child) } } for (const el of menu){ handleElement(el) }