Tengo un ID único que necesito encontrar en un objeto padre con muchos hijos de hijos.
El campo a encontrar sería menuId, ese identificador puede estar tanto en padre como en los hijos children, y si no se encuentra en primer objeto avanzar al segundo. Ya lo he intentando de muchas formas pero sigue sin funcionarme, es conveniente usar un método recursivo. Cabe aclarar que el objecto children tiene los mismos atributos que el método padre.
Más o menos cree algo así.
function handleHiera2(menuId, menuParent) {
const response = [];
let cont = 0;
let key = 0;
let value = 0;
if (menuParent && menuParent.menuId != null) {
if (menuParent.menuId == menuId) {
response.push(menuParent);
}
}
if (menuParent.children && menuParent.children.length > 0) {
for ([key, value] of Object.entries(menuParent.children)) {
if (value.menuId == menuId) {
response.push(value);
}
}
handleHiera2(menuId, menuParent.children);
} else {
cont += 1;
const list = scope.listHierarchy[cont + 1];
handleHiera2(menuId, list);
}
}
}
Este es el objecto JSON que trae; eliminé varios child ya que no me deja subir mucho acá, pero la composición es parecida, la diferencia es que dentro de children pueden haber otros hijos
[
{
"menuId": "1ca36ec2-31cf-4e3b-b499-59315f953a39",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "Producción",
"reportName": null,
"workspace": null,
"dashBoardName": null,
"creationUtcDate": "2021-01-27T18:46:29.923",
"modificationUtcDate": "2021-01-27T18:46:29.923",
"menuParentId": "f77da472-63c7-4318-a451-233664626df5",
"isParent": true,
"menuOrder": 9,
"children": [
{
"menuId": "f9b8109b-ef77-4e23-9c6e-1be8484e3568",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "Anomalías Temperatura",
"reportName": "EO MLT AnomaliasTemperatura v1.0",
"workspace": "VU_Production",
"dashBoardName": null,
"creationUtcDate": "2021-01-28T15:09:58.44",
"modificationUtcDate": "2021-01-28T15:09:58.44",
"menuParentId": "1ca36ec2-31cf-4e3b-b499-59315f953a39",
"isParent": false,
"menuOrder": 2,
"children": [],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true
},
{
"menuId": "b1704042-66f2-4b82-acf2-24d4b63431fc",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "Comparación de curvas de probabilidad - Rosa",
"reportName": "EO-IACPO_CurvasProbabilidadRosa_V1.0",
"workspace": "VU_Production",
"dashBoardName": null,
"creationUtcDate": "2021-02-01T17:00:59.193",
"modificationUtcDate": "2021-02-01T17:00:59.193",
"menuParentId": "1ca36ec2-31cf-4e3b-b499-59315f953a39",
"isParent": false,
"menuOrder": 3,
"children": [],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true
},
],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true,
"$$hashKey": "object:394"
},
{
"menuId": "cac1dbd6-012b-46cc-805a-f28f43bcbe96",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "Supply Chain",
"reportName": null,
"workspace": null,
"dashBoardName": null,
"creationUtcDate": "2021-01-20T14:25:37.427",
"modificationUtcDate": "2021-01-20T14:25:37.427",
"menuParentId": "b9557aa7-8cad-488a-abcd-ff24cb8cee6d",
"isParent": true,
"menuOrder": 8,
"children": [
{
"menuId": "fa15d9d6-dd57-4f7f-80cc-5c1f0809b208",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "BSC Logística",
"reportName": "BSC Logística - Val 2021",
"workspace": "VU_SupplyChain",
"dashBoardName": null,
"creationUtcDate": "2021-01-25T14:17:13.327",
"modificationUtcDate": "2021-01-25T14:17:13.327",
"menuParentId": "cac1dbd6-012b-46cc-805a-f28f43bcbe96",
"isParent": false,
"menuOrder": 1,
"children": [
{
"menuId": "c69d248f-3aa1-4f0f-9ad7-d0d740774bed",
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"formId": null,
"name": "Dashboard QU",
"reportName": "Dashboard QU 5-15",
"workspace": "VU_SupplyChain",
"dashBoardName": null,
"creationUtcDate": "2021-01-20T14:09:35.61",
"modificationUtcDate": "2021-01-20T14:09:35.61",
"menuParentId": "cac1dbd6-012b-46cc-805a-f28f43bcbe96",
"isParent": false,
"menuOrder": 2,
"children": [],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true
},
],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true
},
],
"form": null,
"icon": {
"iconId": "7fde6368-7686-4de8-86b2-e667c7767ec0",
"icon": "iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZ1JREFUeNrcVcFNw0AQjJH/uAPcAUkFCRUQpPxjf/gmrgBSQf58Ev95uIM4FWBXQKggpgKYRXPoZN3ZdzEvTlpZ3vPd7M7Orkej/7IC/eX29THG4wCLPe5oYGm9eClsH4St9wRWwXIPkGvYCmYFuTL4atiTI8AS9tn3UWjbQPrPGo1jPN5Iy17zT10iCR0jXql7bR8wEKlPRNcJATU+IBls7EBdrQWSs77OIGsqLu6gN9Oy2vYV3lYfiSp1DKomdd01QTRfBvcZ/rbvaDquqPoFwcGIlCi1bDz65EZoxB2SbYGsK5u6EhYuv2BqfPDclBk82Ogq2ICNnqbHiijz1ERXoNVg5tHppqVqU4Ky0joguxaCOODwnWVvS8qFkRmBUi8Jd6hITW4RzYQXT2BzTgBvENucEpAKACf2lKprdAmIrVFLSjhhZglHUPWXdM2pyh0beKeNouF04dI1L82QUaCMUr5ng/qpS2vWpSgNvrM0X1uymiDeBdQnExk1JU2NncgEwFqJECrpv2BI0VmDTc8/Jh2qrqxn/+fH9S3AAND3lQL1q8ymAAAAAElFTkSuQmCC",
"isParent": false,
"menus": []
},
"menuParent": null,
"permissions": [],
"permission": {
"permissionId": "00000000-0000-0000-0000-000000000000",
"menuId": "00000000-0000-0000-0000-000000000000",
"view": false,
"edit": false,
"create": false,
"delete": false,
"userId": "00000000-0000-0000-0000-000000000000",
"menu": null,
"user": null
},
"isActive": false,
"reportFilters": [],
"show": true,
"$$hashKey": "object:395"
}
]
Lo que debe devolver es si encuentro dentro de un objeto hijo el resultado. Devolver todo el array completo que lo contiene para poder listarlo en un select.