Por favor ayuda,
Quiero hacer un método findChildByIdInShop(shop:any, childId:string) donde la tienda es cualquier nodo principal JSON que tenga hijos con Id.
Simplemente, cómo hacer un método que reciba JsonNode y su ID secundario como parámetros para encontrar ese objeto usando angular.
En mi caso, tengo una tienda como nodo donde quiero encontrar los objetos de sus hijos (pueden estar anidados) por id.
Gracias por tu tiempo :)
Servicio de back-end
createShop(id: string): any { let shop: any = {}; shop.id = id; shop.departmentList = []; let dep1: any = {}; dep1.id = "d1" dep1.name = "my department 1"; shop.departmentList.push(dep1); let dep2: any = {}; dep2.id = "d2" dep2.name = "my department 2"; dep2.departmentVariationList = []; let dep2v1: any = {}; dep2v1.id = "dep2v1" dep2v1.name = "my department variation dep2v1"; dep2.departmentVariationList.push(dep2v1); //shop.departmentList.push(dep2); let dep2v2: any = {}; dep2v2.id = "dep2v2" dep2v2.name = "my department variation dep2v2"; dep2.departmentVariationList.push(dep2v2); shop.departmentList.push(dep2); let dep3: any = {}; dep3.id = "d3" dep3.name = "my department 3"; shop.departmentList.push(dep3); return shop; }Encuentre a cualquier niño en la tienda por su método de identificación en el servicio de backend:
findChildByIdInShop(shop:any, childId:string):any[]{ // how to implement this }Lo que he intentado pero sin resultado:
findNodeInArray(array:any,id:string):any{ let node:any; for(let i=0; i<array.length; i++){ let n=array[i]; if(n.id == id) { node= n; break; } for (const k in n) { const v = n[k]; if(v instanceof Array){ this.findNodeInArray(v, id); } } } return node; } findNode(parentNode:any,id:string):any { if (parentNode instanceof Array) { this.findNodeInArray(parentNode, id); } for (const k in parentNode) { const v = parentNode[k]; if(v instanceof Array){ this.findNodeInArray(v, id); } else { return "Undefined";} } }En el componente frontal:
let shop: any = this.backendService.createShop("shop1"); let node: any = this.backendService.findNode(shop, "dep2v2"); let node2: any = this.backendService.findNode(shop, "dep2v1"); console.log(JSON.stringify("Shop1 node", node)); console.log(JSON.stringify("Shop2 node", node2));Para implementar lodash
https://stackoverflow.com/a/41992126/18762612
import { get } from "lodash";variable en componente
deepObj = {a:{b:{n:{e:100, c: 120}}}ejemplo de obtener
constructor(){ get(deepObj, 'abne'); }Obtuve el resultado utilizando el método Recursively Traverse an Object: este enlace me ayuda: https://cheatcode.co/tutorials/how-to-recursively-traverse-an-object-with-javascript .
el servicio de backend:
// Verify the object isObject(value): any { return !!(value && typeof value === "object"); }; // Find object in node findNestedObject(object: {}, keyToMatch: String, valueToMatch: String): any { if (this.isObject(object)) { let entries = Object.entries(object); for (let i = 0; i < entries.length; i += 1) { const [objectKey, objectValue] = entries[i]; if (objectKey === keyToMatch && objectValue === valueToMatch) { return object; } if (this.isObject(objectValue)) { let child = this.findNestedObject(objectValue, keyToMatch, valueToMatch); if (child !== null) { return child; } } } } return null; };y llame a ese método en el componente como:
// Find the nested object by passing node, key, & value // the this.shop is your backend data or json let result = this.backendService.findNestedObject(this.shop, "id", "dep2v2"); console.log("Result: ", result);