Search code examples
jsonangularangular7

How to display data from JSON format?


I need loop which can print data in following hierarchy solutiondetail -> groups - > request details The following function was giving me details of solutiondetail -> request detail. I have given both new and old json format along with working function for old json. Now after adding group detail I am facing issue with loop.

Previous Json : solutiondetail -> request

{
    "SolutionsDetail": [
        {
            "SolutionId": 658,
            "name": "dk",
            "id": 1568377327000,
            "requestDetails": [
                        {
                            "ReqId": 2331,

                        },

                    ]
        }
    ]
}

GeneartingLandingPadData(nxsolId) {
   for (let index = 0; index < this.json.length; index++) {
      if (this.array[index].SolutionId == nxsolId) {
        this.priceDetails = this.array[index].requestdetails;
             for (let k = 0; k < this.priceDetails.length; k++) {
                    console.log("some",this.priceDetails);
                }

            }
        }
        return false;
    }

New Json : solutiondetail -> groups - > request details

 {
        "SolutionsDetail": [
            {
                "SolutionId": 658,
                "name": "dk",
                "id": 1568377327000,
                "groups": [
                    {
                        "GroupId": 1,
                        "requestDetails": [
                            {
                                "ReqId": 2331,

                            },

                        ]
                    }

                ]
            }
        ]
    }

Solution

  • With the limited amount of information which you have provided, I cannot recreate the problem to test it. But from what I understood I think changing your function like below might solve your problem. With the new JSON, you have to access groups array first. Then proceed to requestId. Even if the following code doesn't exactly work hopefully you will be able to figure it out by first accessing group array and then trying for the request id.

    GeneartingLandingPadData(nxsolId) {
           for (let index = 0; index < this.json.length; index++) {
              if (this.array[index].SolutionId == nxsolId) {
                var groupDetails = this.array[index].groups;
                     for (let k = 0; k < groupDetails.length; k++) {
                         var priceDetails = groupDetails[k].requestDetails;
                         for (let j = 0; j < priceDetails.length; j++){
                             console.log("some", priceDetails);
                         }
                     }
                }
            }
            return false;
        }