locked
Get parent object of Json object using child object angularjs RRS feed

  • Question

  • User2108892867 posted

    Hello everyone, I have this object: 

    "orders" : [
        {
          "ordernum" : "PRAAA000000177800601",
          "buyer" : "Donna Heywood"
          "parcels" : [
            {
              "upid" : "1",
              "tpid" : "2"
            },
            {
              "upid" : "3",
              "tpid" : "4"          
            }
          ]
        },
        {
          "ordernum" : "ORAAA000000367567345",
          "buyer" : "Melanie Daniels"
          "parcels" : [
            {
              "upid" : "5",
              "tpid" : "6"
            },
            {
              "upid" : "7",
              "tpid" : "8"
            }
          ]
        }
      ]

    Supposed I am able to get this object 

    {
              "upid" : "5",
              "tpid" : "6"
            },

    How do I move up one level to grab the parent object of this child object? So I am trying to get this whole object like so:

    {
          "ordernum" : "ORAAA000000367567345",
          "buyer" : "Melanie Daniels"
          "parcels" : [
            {
              "upid" : "5",
              "tpid" : "6"
            },
            {
              "upid" : "7",
              "tpid" : "8"
            }
          ]
        }

    Thanks

    Wednesday, October 3, 2018 2:44 AM

All replies

  • User61956409 posted

    Hi asplearning,

    You can use the following code to achieve your requirement.

    <script>
        var orderlist = {
            "orders": [
                {
                    "ordernum": "PRAAA000000177800601",
                    "buyer": "Donna Heywood",
                    "parcels": [
                        {
                            "upid": "1",
                            "tpid": "2"
                        },
                        {
                            "upid": "3",
                            "tpid": "4"
                        }
                    ]
                },
                {
                    "ordernum": "ORAAA000000367567345",
                    "buyer": "Melanie Daniels",
                    "parcels": [
                        {
                            "upid": "5",
                            "tpid": "6"
                        },
                        {
                            "upid": "7",
                            "tpid": "8"
                        }
                    ]
                }
            ]
        };
    
        var mydata = {
            "upid": "5",
            "tpid": "6"
        };
    
        orderlist.orders.forEach(function (order) {
            var result = order.parcels.filter(customfilter);
    
            if (result.length > 0) {
                alert(JSON.stringify(order));
            }
    
        });
    
    
        function customfilter(item) {
            if (item.upid == mydata.upid && item.tpid == mydata.tpid) {
                return true;
            }
            return false;
        }
    </script>

    Test Result:

    With Regards,

    Fei Han

    Thursday, October 4, 2018 6:32 AM
  • User2108892867 posted

    Thanks Fei Han. Do you know if there is something like .parent to get the parent object of an object? The reason is I have like 20 fields in the child object and doing a comparison for the all the fields in this method doesn't seem practical.

    function customfilter(item) {
            if (item.upid == mydata.upid && item.tpid == mydata.tpid &&  .... && .. &&) {
                return true;
            }
            return false;
        }

    Thanks

    Thursday, October 4, 2018 8:15 PM
  • User475983607 posted

    The original code has syntax errors but can you explain why you do not know the parent object?  For example, the parcels collection belongs to 2nd item in the order collection.

    var myType = {
                "orders": [
                    {
                        "ordernum": "PRAAA000000177800601",
                        "buyer": "Donna Heywood",
              "parcels": [
                            {
                                "upid": "1",
                                "tpid": "2"
                            },
                            {
                                "upid": "3",
                                "tpid": "4"
                            }
                        ]
                    },
                    {
                        "ordernum": "ORAAA000000367567345",
                        "buyer": "Melanie Daniels",
              "parcels": [
                            {
                                "upid": "5",
                                "tpid": "6"
                            },
                            {
                                "upid": "7",
                                "tpid": "8"
                            }
                        ]
                    }
                ]
            };

    To get to {upid: "5", tpid: "6"} you need to know the index of the or the parcels of the orders...

    myType.orders[1].parcels[0]

    Generally, this is drilled into using a for...loop or maybe an $.each() in jQuery.  How is it possible that you do not know the index of the current array item that you are working on?

    Thursday, October 4, 2018 8:34 PM
  • User2108892867 posted

    Thank you mgebhard, what I want to achieve is the other way around. I have this

     {upid: "5", tpid: "6"}

    and I want to get the parent. It's kind of drilling up rather than down. In jQuery, to find the parent container, there is a .parent function. Is there something similar to this? 

    Thursday, October 4, 2018 10:00 PM
  • User475983607 posted

    Thank you mgebhard, what I want to achieve is the other way around. I have this

     {upid: "5", tpid: "6"}

    and I want to get the parent. It's kind of drilling up rather than down. In jQuery, to find the parent container, there is a .parent function. Is there something similar to this? 

    It's not possible if  {upid: "5", tpid: "6"} is all you have.  There are no parents and you must invoke a find as suggested by Fei Han.  But if you started with the entire object, then you have a logical error in your code as it is not possible to to not know what loop you are in.

    Perhaps post code that reproduces the issue.

    Thursday, October 4, 2018 10:40 PM
  • User-474980206 posted
    The dom tree has a parent pointer, that is used to walk up the tree. Your data structure does not, but you could add a parent property to each child, an set it when the child is added. If the data comes from json, then you will need a post processing step, and json does not support object pointers.
    Friday, October 5, 2018 12:28 AM
  • User2108892867 posted

    Thank you Bruce. Yes adding parent property maybe one option. 

    Regards

    Friday, October 5, 2018 2:59 AM