简体   繁体   中英

Angular JS overriding copied object in loop

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM