0

I am trying to copy an object into another object in loop. But while iteration the copied object is overriding by the last object in loop. My code looks like

 $scope.createDashboradsforeachTab = function(dashboardData, eachTab) {  
        var dashboardslayoutArray = eachTab.dashlettes;
        dashboardData.dashletteBeansList = eachTab.dashlettes;  
        angular.forEach(dashboardslayoutArray,function(dashboardslayout) {
      dashboardData.dashletteBeansList = [];
      dashboardData.dashletteBeansList[0] = dashboardslayout;
      var dashletteIdInTab = dashboardslayout.dashletteId;
      if (dashboardslayout.dashletteId == 12) {
         $scope.dashboardDataForDrillDown = {};
         $scope.dashboardDataForDrillDown =  angular.copy(dashboardData);
      }
       //Rest of my code here
      });

    }

you can clearly see now eachTab contains set of dashlettes. Now i am iterating through each dashlette, while iteration i am assign dashlette object to dashboardData object. While iteration iam copying for dashboardData object to another object for dashlette id 12. As now now it is copied correctly I have checked in debug . in the next iteration dashletted id is 13 and i am assigning it to dashboardData object. Now my copied object is also got affected with new dashlette (13)object. Please help me out on this, how to stop overriding my copied object.

2
  • May be apply a check above DashboardsDataService.getTabDetails(dashboardData) call so it gets only called for specific condition. Commented Sep 9, 2015 at 5:04
  • Actucally that method calls for each dashboard having in each tab. If my tab contains 4 dash boards , it has to be called for 4 times to get each dashboard data from back end. But dashboardData object differ for each dashboard. Before calling service only dashboard Data object changed based on the dashboard type in each tab. Commented Sep 9, 2015 at 5:13

1 Answer 1

1

In general, your filter condition might be wrong based on your business logic. But bold guess here is use strict equality check === for the comparison also as you mentioned that your

$scope.dashboardDataForDrillDown

contains last 8th item data. it means the condition is true for all times. you can debug your condition in console for further proof.

Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my code, you can clearly see now eachTab contains set of dashlettes. Now i am iterating through each dashlette, while iteration i am assign dashlette object to dashboardData object. While iteration iam copying for dashboardData object to another object for dashlette id 12. As now now it is copied correctly I have checked in debug . in the next iteration dashletted id is 13 and i am assigning it to dashboardData object. Now my copied object is also got affected with new dashlette (13)object.
have you tried with strict equality check === ? Also if you can give us the exact object structure it would be beneficiary to solve your problem quickly like this dashboardData = [ dashlettes :{} ]; eachTab = [ dashlettes :{} ]; or any quick plunker. I feel that based onto current code in part of looping your are overriding the object itself. though i need to check the structure of your object

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.