0

Please help,

I want to make a method findChildByIdInData(data:any, childId:string) where the data is any JSON main node that has children with Ids.

Simply, how to make a method that receives JsonNode and its child Id as parameters to find that object using angular.

In my case, I have a data as a node where I want to find its children's objects (may be nested) by id.

Thanks for your time :)

2 Answers2

0

To implement lodash

https://stackoverflow.com/a/41992126/18762612

import { get } from "lodash";

variable in component

deepObj = {a:{b:{n:{e:100, c: 120}}} 

example of get

constructor(){
 get(deepObj, 'a.b.n.e');
}
  • Thank you @Nathash for your response :). May I know what if I add another nested object in `[{ id: 'Dep1',name: 'My Department 1', departmentVariationList: [{ id: 'nestedDep1', name: 'My Department 1',}]` How to reach this nested object? – ftslaptop pc May 13 '22 at 17:21
  • I have update the findChild function pls check now – Nathash Kumar May 13 '22 at 17:45
  • Thanks for your all efforts. can you see this https://www.youtube.com/watch?v=zKnpzRjicKc it's very similar what I want to achieve in my code.. – ftslaptop pc May 13 '22 at 18:04
  • I am sorry but I am a newbie to angular and programming, so I am stuck to make how we can get any (child, grandChild, greatGrandChild and so on..) nested object by its id using one method. – ftslaptop pc May 13 '22 at 18:16
  • As per video - this will help you https://lodash.com/docs/#get – Nathash Kumar May 14 '22 at 05:53
  • Hey! Nathash Kumar, I am very thankful to you as I learned new code from you. Thanks again for your interest and quick response to my question buddy! – ftslaptop pc May 14 '22 at 08:47
0

Got the result by using the Recursively Traverse an Object method: this link helps me: https://cheatcode.co/tutorials/how-to-recursively-traverse-an-object-with-javascript.

the backendService:

// 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;
      };

and call that method in component as:

 // Find the nested object by passing node, key, & value
        // the this.shop is your backend data or json  
        let  result = this.backendService.findNestedObject(this.data, "id", "dataId");
        console.log("Result: ", result);