2

I have add my code and json which I want to loop. I have looped $scope.data using foreach loop and push to $scope.tempObj, its working fine. But when I try to loop $scope.data.policyDocumentContentCollection and push to $scope.tempObj.nested, its get console error "Cannot read property 'push' of undefine". Can I know how to push 2nd foreach data to nested array which is in $scope.tempObj.

$scope.data = [
   {
      "policyNo":"DBDP18S016696",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016697",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016698",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   }
]



$scope.tempObj = [];

                    angular.forEach($scope.data, function (data) {
                        $scope.tempObj.push({
                            'id': data.policyNo,
                            'renewalNo': data.renewalNo,
                            'endorsementNo': data.endorseNo,
                            'product': data.productDesc,
                            'insuredName': data.holderName,
                            'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
                            'nested': []
                        });
                        angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
                            $scope.tempObj.nested.push({
                                docName: docs.docType
                            })
                        });
                    });
2
  • 1
    $scope.tempObj.nested doesn't exist. $scope.tempObj is an array. Commented Jan 18, 2019 at 12:18
  • i think you have in the second loop you have to loop $scope.data and next you loop $scope.data[key].policyDocumentContentCollection Commented Jan 18, 2019 at 12:19

2 Answers 2

3

You can create an intermediate object to be able to push inside the current element. Right now you can't because you push on $scope.tempObj.nested which does not exist, only $scope.tempObj[index].nested exist because $scope.tempObj is an array.

angular.forEach($scope.data, function(data) {
  const element = {
    id: data.policyNo,
    renewalNo: data.renewalNo,
    endorsementNo: data.endorseNo,
    product: data.productDesc,
    insuredName: data.holderName,
    periodOfInsurance: data.fromDate + " to " + data.toDate,
    nested: []
  };

  angular.forEach(data.policyDocumentContentCollection, function(docs) {
    element.nested.push({
      docName: docs.docType
    });
  });

  $scope.tempObj.push(element);
});
Sign up to request clarification or add additional context in comments.

Comments

0

$scope.tempObj.nested does not exist because the $scope.tempObj is an array of object. So you have to first access the object at a specific index of $scope.tempObj array and then push the value into $scope.tempObj[index].nested array.

$scope.data = [
   {
      "policyNo":"DBDP18S016696",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016697",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016698",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   }
]



$scope.tempObj = {};

                    angular.forEach($scope.data, function (data, index) {
                        $scope.tempObj = {
                            'id': data.policyNo,
                            'renewalNo': data.renewalNo,
                            'endorsementNo': data.endorseNo,
                            'product': data.productDesc,
                            'insuredName': data.holderName,
                            'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
                            'nested': []
                        };
                        angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
                            $scope.tempObj[index].nested.push({
                                docName: docs.docType
                            })
                        });
                    });
j

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.