如何制作一种方法,通过 id 在 angular 中查找每个对象
How to make a method that finds every object by id in angular
请帮忙,
我想创建一个方法findChildByIdInShop(shop:any, childId:string)
,其中商店是任何 JSON 具有带 ID 子节点的主节点。
简单地说,如何创建一个接收 JsonNode 及其子 Id 作为参数的方法,以使用 angular 找到该对象。
在我的例子中,我有一个商店作为节点,我想通过 id 找到它的子对象(可能是嵌套的)。
感谢您的宝贵时间:)
后端服务
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;
}
通过后端服务中的 Id 方法在商店中查找任何儿童:
findChildByIdInShop(shop:any, childId:string):any[]{
// how to implement this
}
我试过但没有结果:
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";}
}
}
前面组件:
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));
实现lodash
import { get } from "lodash";
组件中的变量
deepObj = {a:{b:{n:{e:100, c: 120}}}
获取示例
constructor(){
get(deepObj, 'a.b.n.e');
}
使用递归遍历对象方法得到结果:
这个 link 帮助我:https://cheatcode.co/tutorials/how-to-recursively-traverse-an-object-with-javascript.
后端服务:
// 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;
};
并在组件中调用该方法:
// 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);
请帮忙,
我想创建一个方法findChildByIdInShop(shop:any, childId:string)
,其中商店是任何 JSON 具有带 ID 子节点的主节点。
简单地说,如何创建一个接收 JsonNode 及其子 Id 作为参数的方法,以使用 angular 找到该对象。
在我的例子中,我有一个商店作为节点,我想通过 id 找到它的子对象(可能是嵌套的)。
感谢您的宝贵时间:)
后端服务
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;
}
通过后端服务中的 Id 方法在商店中查找任何儿童:
findChildByIdInShop(shop:any, childId:string):any[]{
// how to implement this
}
我试过但没有结果:
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";}
}
}
前面组件:
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));
实现lodash
import { get } from "lodash";
组件中的变量
deepObj = {a:{b:{n:{e:100, c: 120}}}
获取示例
constructor(){
get(deepObj, 'a.b.n.e');
}
使用递归遍历对象方法得到结果: 这个 link 帮助我:https://cheatcode.co/tutorials/how-to-recursively-traverse-an-object-with-javascript.
后端服务:
// 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;
};
并在组件中调用该方法:
// 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);