Search code examples
angularjsangularjs-scope

angularjs 1.2 - merge json obj to another json obj


I make two http request and I have to merge the second HTTP call's response to the first response NOTE: Second response has data for each individualID. So I tried appending using angular.forEach function.. I couldn't get it right

RESPONSE FROM FIRST HTTP REQUEST is stored in $scope.bobData ===================================================

$scope.bobData =   {
        "count": 1,
        "listOfIndividuals": [{
            "individualId": "1234",
            "firstName": "UAT Michael",
            "lastName": "Brady",
            "address1": "4950 Alhambra Valley Rd",
            "premium": "886.48",
            "pageNumber": 0,
            "sortBy": null,
            "sortOrder": null,      
        },
    {
            "individualId": "1235",
            "firstName": "Mike",
            "lastName": "Randy",
            "address1": "1300 Mission Valley Rd",
            "premium": "990.00",
            "pageNumber": 0,
            "sortBy": null,
            "sortOrder": null,      
        }
    ]
        }

RESPONSE FROM SECOND HTTP REQUEST is stored in $scope.individualPlanData

$scope.individualPlanData=     {
            "individualPlanDetails": {
                "1234": {
                    "planId": "p01",
                    "noOfEnrollees": 0,
                    "officeVisit": null,
                    "genericDrugs": null,
                    "deductible": null,
                    "issuerLogo": null,
                    "issuerName": null,
                    "planName": null,
                    "planType": null
                },
        "1235": {
                    "planId": "p01",
                    "noOfEnrollees": 0,
                    "officeVisit": null,
                    "genericDrugs": null,
                    "deductible": null,
                    "issuerLogo": null,
                    "issuerName": null,
                    "planName": null,
                    "planType": null
                }
            }
        }
CODE SNIPPET::


angular.forEach(vm.bobData, function(obj) { 
                angular.forEach(vm.individualPlanData.individualPlanDetails[key], function (value, key) {
                    obj.officeVisit: value.officeVisit,
                    obj.genericDrugs: value.genericDrugs,
                    obj.deductible: value.deductible,
                    obj.issuerLogo: value.issuerLogo,
                    obj.issuerName: value.issuerName,
                    obj.planName: value.planName,
                    obj.planType: value.planType
                })
            });

EXPECTED RESULT:

$scope.bobData =   {
        "count": 1,
        "listOfIndividuals": [{
            "individualId": "1234",
            "firstName": "UAT Michael",
            "lastName": "Brady",
            "address1": "4950 Alhambra Valley Rd",
            "premium": "886.48",
            "pageNumber": 0,
            "sortBy": null,
            "sortOrder": null,
            "1234": {
                    "planId": "p01",
                    "noOfEnrollees": 0,
                    "officeVisit": null,
                    "genericDrugs": null,
                    "deductible": null,
                    "issuerLogo": null,
                    "issuerName": null,
                    "planName": null,
                    "planType": null
                },
              },
             {
            "individualId": "1235",
            "firstName": "Mike",
            "lastName": "Randy",
            "address1": "1300 Mission Valley Rd",
            "premium": "990.00",
            "pageNumber": 0,
            "sortBy": null,
            "sortOrder": null,
            "1235": {
                    "planId": "p01",
                    "noOfEnrollees": 0,
                    "officeVisit": null,
                    "genericDrugs": null,
                    "deductible": null,
                    "issuerLogo": null,
                    "issuerName": null,
                    "planName": null,
                    "planType": null
                }
            }

        }
    ]

}


Solution

  • angular.forEach(bobData.listOfIndividuals, function(response1Value, response1Key) { 
                            angular.forEach(response1Value, function(listOfIndividualsValue, listOfIndividualsKey){
                                if(listOfIndividualsKey === "individualId") {
                                    angular.forEach(individualPlanData.individualPlanDetails, function(response2Value, response2Key){
                                        if (listOfIndividualsValue === response2Key) {
                                            response1Value.planType = response2Value.planType;
                                            response1Value.officeVisit = response2Value.officeVisit;
    
    
                                        }
                                    });
                                }
                            });
                        });