0

I have two variables in my controllers in AngularJS i.e

_this.currentData=new Array();
_this.masterCurrentData=new Array();

Later I created another temporary Array of Objects and filled separately using loops etc.

var tmpDataSort=[{
    "values":new Array(),
             disabled: false,
            "key":"Retail"
        },{
            "values":new Array(),
             disabled: true,
            "key":"Commercial"
        },{
            "values":new Array(),
             disabled: true,
            "key":"Industrial"
        },{
            "values":new Array(),
             disabled: true,
            "key":"Residential"
        }]
_this.masterCurrentData=tmpDataSort;
_this.currentData=tmpDataSort;
tmpDataSort=null;

Later whenever I am performing an operation on Current Data automatically the value of MasterCurrentData is changing and I have never used masterCurrent Data

_this.modifyCurrentBean = function(locName, selectFlag){
    console.log(_this.masterCurrentData[0].values)
        for(var i=0;i<_this.currentData.length;i++){
            for(var j=0;j<_this.currentData[i].values.length;j++){
                if(_this.currentData[i].values[j].x==locName){                      
                    _this.currentData[i].values.splice(j,1);
                    break;
                }
            }
        }
        console.log(_this.masterCurrentData[0].values)
    }

The first console shows [object object] and the second console show only an single [object]

3
  • Can you make a demo, so we could try something to help you. Commented Jan 5, 2016 at 9:05
  • Well since currentData and masterCurrentData refer to the same array. Calling splice on the array will affect both. Commented Jan 5, 2016 at 9:18
  • currentData and masterCurrentData point at the same array. Commented Jan 5, 2016 at 9:19

1 Answer 1

2

You assign tmpDataSort to _this.currentData and _this.masterCurrentData, so by changing _this.currentData you change tmpDataSort and _this.masterCurrentData as well. You can make a copy of tmpDataSort using _this.currentData=angular.copy(tmpDataSort);

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

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.