In AngularJS, variables can undergo automatic value changes without any external connections

Within my AngularJS controllers, I have initialized two variables:

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

Later on, I created a temporary array of objects called tmpDataSort and populated it separately using loops.

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;

However, I noticed that whenever I perform an operation on Current Data, the value of MasterCurrentData changes even though I never explicitly use masterCurrentData.

_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 a single [object]

Answer №1

When you assign tmpDataSort to both _this.currentData and _this.masterCurrentData, any changes made to _this.currentData will also affect tmpDataSort and _this.masterCurrentData. To create a separate copy of tmpDataSort, you can use

_this.currentData=angular.copy(tmpDataSort);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for installing a package specified as a peerDependency?

I'm in the process of creating a library and I'm looking to figure out how to specify and install a dependency under peerDependencies. I couldn't find any information about this in the npm documentation when using the command npm install: ...

Implementing a hamburger menu and social media sharing buttons on websites

Currently working on my personal website and delving into the world of Web Development, I have some inquiries for seasoned developers. My first query revolves around incorporating a hamburger menu onto my site for easy navigation to other pages. After att ...

Are the import and export keywords native to webpack or are they specific to JavaScript syntax?

I am pondering whether the import & export aspects are part of the language itself or simply keywords that webpack adds to the language. Thank you. ...

The search functionality for the MongoDB module is not functioning properly within my Next.js application

Working on my nextjs application, I have encountered an issue with using the mongodb node module to find all documents in one of my collections. Despite successful usage of .findOne and .updateOne for other pages like login and password reset, when I use . ...

Extra brackets appear when retrieving JSON data

In my code, I am attempting to extract data from a JSON file. The JSON data does not have any brackets, just an array separated by commas. However, when I retrieve the data using $scope, an extra square bracket is added outside of my output. Here is a demo ...

Retrieve the maximum numerical value from an object

My goal is to obtain the highest value from the scores object. I have a global object called "implementations": [ { "id": 5, "project": 'name project', "scores": [ { "id": 7, "user_id": 30, "implement ...

Formatting text to automatically continue onto the next line without requiring scrolling through long blocks of

I have a unique Angular project with a terminal interface that functions properly, maintaining a vertical scroll and automatically scrolling when new commands are entered. However, I am struggling to get the text within the horizontal divs to wrap to the ...

Display HTML content using AJAX within a div element

Within my HTML code, I have the following: <li class="register"><a href="">Registreer</a></li> as well as <div id="content"> </div> I attempted to load an HTML file into the div using the code below in the header se ...

Need to battle with... its own contradictions in a direct manner

I've integrated APIs for my bot, expecting them to work smoothly. However, I'm still struggling to figure out why the server keeps aborting itself... Here is the error output, code, and a screenshot. Any help would be greatly appreciated. Error: ...

Troubleshooting script not detecting changes in form

I am currently implementing a script to detect any changes in the form fields and notify the user if there are any. However, instead of triggering the alert box as intended, a different JavaScript box is displayed. Despite my efforts, the script is not re ...

What methods can be used to retrieve text data from a website built with Angular technology?

Exploring new territories with Angular and Selenium, I find myself facing a challenge in extracting specific text fields from a website. The precise value seems elusive within the intricate web of HTML code. Seeking guidance or tips from experienced indivi ...

The element does not recognize the property 'width' since it is not defined in the type of 'GlobalEventHandlers'

I'm trying to determine the size of an image using JavaScript, but I encountered a TypeScript error: const img = new Image(); img.onload = function() { alert(this.width + 'x' + this.height); } img.src = 'http://www.google.com/intl/en_ ...

What are the steps to implement session storage within a Puppeteer automation script?

Currently, I am attempting to retrieve the encounter id from a Puppeteer script that launches a browser. These scripts are executed on AWS synthetic canaries. However, when running the script, I encounter the following error: sessionStorage is not define ...

What is the recommended method for writing JavaScript scripts with AJAX in a Rails application? How can URLs be incorporated into the script effectively?

When incorporating AJAX into my Rails application, I encounter difficulties when specifying the URL for the request within a script. While it is recommended to use helpers like my_resource_path instead of manually writing paths, these helpers do not functi ...

What is the best method for converting a plist file into a json file?

Is there a way to convert an existing plist file into a JSON file using one of the following programming languages: JavaScript, Java, Objective-C, Python, or Ruby? Any suggestions on how to do this? ...

Merging Columns in PySpark Dataframe Using an Array of Columns for Input

I have a dataframe with 10 columns and I need to perform concatenation based on an input array of columns: arr = ["col1", "col2", "col3"] This is what I have so far: newDF = rawDF.select(concat(col("col1"), col("col2"), col("col3"))).exceptAll(updateDF. ...

Passing a variable to a template in Node.js and express with hbs is causing an unexpected identifier error

I’m working on a Node.js Express app, and I’m trying to pass a variable when rendering my index.hbs file, like this: <!DOCTYPE html> <html> <body> Hello. <a href="/auth/facebook">Login with Facebook</a> {{ ...

The Angular-Meteor package running on an Android device is encountering an error message stating "Unexpected token <"

Everything was running smoothly with my Angular-Meteor Cordova application until a minor change caused some trouble. Now, when I try to run meteor run android-device, the app attempts to start on the device but encounters issues loading the packages. Here& ...

Ensure the video fills the entire width of its parent element and adjusts its height accordingly to maintain a 16:9

I am looking to make both videos fill 100% width of their parent element while keeping their aspect ratio intact. The parent element takes up 50% of the window's width, so the videos need to be responsive. I have come across numerous solutions that ...

Different time parameter for setting a timeout in Javascript

I am struggling to create a slideshow with varying time intervals for each image. My knowledge of Javascript is limited, and I have been working on resolving this issue for quite some time. /* function ShowEffect(element){ new Effect.Appear(element, ...