简体   繁体   中英

How to get parent object from child sub objects

I have a have JSON object with all child and parent info .

let shaFamily = {
    "name": "Shan",
    "wifeName": "Anga",
    "isMarried": true,
    "childrens": {
      "son": [{
          "name":"Chit",
          "wifeName": "Amba",
          "isMarried": true,
          "genration":1,
          "childrens": {
            "son":[
              {
                "name":"Vritha",
                "genration":2,
                "isMarried": false
              }
            ],
            "doughter": [
              {
                "name":"Dritha",
                "husbandName": "Jaya",
                "genration":2,
                "isMarried": true,
                "childrens": {
                  "son": [
                    {
                      "name":"Yodhan",
                      "isMarried": false
                    }
                  ],
                  "doughter": []
                }
              },
              {
                "name":"Tritha",
                "genration":2,
                "isMarried": false
              }
            ]
          }
        },
        {
          "name":"Ish",
          "genration":1,
          "isMarried": false
        },
        {
          "name":"Vich",
          "genration":1,
          "wifeName": "Lika",
          "isMarried": true,
          "childrens": {
            "son": [],
            "doughter": [
              {
                "name":"Vila",
                "genration":2,
                "isMarried": false
              },
             {
                "name":"Chika",
                "genration":2,
                "isMarried": false
              }
            ]
          }
        },
        {
          "name":"Aras",
          "genration":1,
          "wifeName": "Chitra",
          "isMarried": true,
          "childrens": {
            "son": [
              {
                "name":"Ahit",
                "genration":2,
                "isMarried": false
              }
            ],
            "doughter": [
               {
                "name":"Jnki",
                "genration":2,
                "husbandName": "Arit",
                "isMarried": true,
                "childrens": {
                  "son": [
                    {
                      "name":"Laki",
                      "genration":3,
                      "isMarried": false
                    }
                  ],
                  "doughter": [
                     {
                      "name":"Lavnya",
                      "genration":3,
                      "isMarried": false
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "doughter": [
        {
          "name":"Satya",
          "genration":1,
          "husbandName": "Vyan",
          "isMarried": true,
          "childrens": {
            "son": [
              {
                "name":"Asva",
                "wifeName": "Satvy",
                "genration":2,
                "isMarried": true,
                "childrens": {
                  "son": [
                   {
                      "name":"Vasa","genration":3,
                      "isMarried": false
                    }
                  ],
                  "doughter": []
                }
              },
              {
                "name":"Vyas",
                "wifeName": "Krpi",
                "genration":2,
                "isMarried": true,
                "childrens": {
                  "son": [
                     {
                      "name":"Kriya","genration":3,
                      "isMarried": false
                    }
                  ],
                  "doughter": [
                    {
                      "name":"Krithi",
                      "genration":3,
                      "isMarried": false
                    }
                  ]
                }
              }
            ],
            "doughter": [
              {
                "name":"Atya",
                "genration":2,
                "isMarried": false
              }
            ]
          }
        }
      ]
    }
}

here from child i wanna get who is his parents .

function checkPerson(data , personName ) {
    var result = 0;
    if( data !== null && typeof data == "object" ) {
        Object.entries(data).forEach(([key, value] ) => {
                if( value === personName  && ( key === "wifeName" || key === "name" || key==="husbandName")){
                    console.log(data);
                }
            if(result) return result;
            else result = checkPerson(value, personName);
        });
    }
    return result;
}

checkPerson(shaFamily , "Vritha" ) Output: Chit Amba

checkPerson(shaFamily , "Yodhan" ) output: Jaya Dritha

from the above JSON i am able to serach person through name after searching name i want the person parents or grandParents. i am not getting how to do that.

There is not way generic way to obtain the parent object in JSON. Take the following example:

var myObj = {
    x: 12,
    y: {
        nestedProp: "hello"
    }
};

If we take the object myObj.y , it has no reference at all to the enclosing myObj . The only way to do that is to do some extra bookkeeping work in the algorithm you're writing. Side note, I also think you shouldn't iterate through all keys to do that name check:

function getParentsHelper(curNode, targetName) {
    if (curNode.name === targetName || curNode.wifeName === targetName || curNode.husbandName === targetName){
        return [curNode]; // base case 1 -- found the name
    }

    if (this.childrens === undefined){
        return null; // base case 2 -- the name wasn't in this leg of the object tree
    }

    for (let i = 0; i < curNode.childrens.length; i++){ // recursive cases!
        let result = getParents(curNode.childrens[i], targetName);
        if (result !== null){ // we found the name in one of our children, add ourselves to the list of parents
            result.unshift(curNode);
            return result;
        }
    }

    return null; // never found that name in the object tree!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM