Changes are also being made to the props value

My dialog component has a student_list props and is triggered by a watcher. Essentially, when I increase the variable dialog_watcher++ in my main component, the dialog will open.

The data within the student_list props looks like this:

student_list = [
   {id: 1, name: 'John'},
   {id: 2, name: 'Jane'},
   {id: 3, name: 'Jake'},
   {id: 4, name: 'Finn'},
]

Within the Dialog component, in my watch:{} hook, there is a variable student_list_data where I assign the value of the student_list props.

watch: {
    let temp = this.student_list;
    this.student_list_data = temp;
}

Within the Dialog component, there is a button that allows me to remove an item from the student_list_data array.

_selectStudent(item) {
    //remove the selected student from the array
    const index = this.student_list_data.findIndex(obj => obj.id == item.id);
    this.student_list_data.splice(index, 1);

    //append the selected student to a new array
    this.selected_student.push(item);
}

Upon inspecting in my vue devtools, I noticed that not only the student_list_data array was spliced, but also the student_list props were affected. I have checked thoroughly but could not find any function that modifies the student_list props directly.

Any suggestions on what might be causing this unexpected behavior?

Answer №1

When non-primitive variables are assigned using the "=" operator, the reference is copied as well. This can result in mutations for all variables that a specific object is assigned to.

Give these solutions a try:

  1. Convert it to a string using JSON.stringify and then parse it with JSON.parse.

    this.data_list = JSON.parse(JSON.stringify(this.list));
    
  2. Another option is to use the spread operator.

    this.data_list = [...this.data_list]
    

Answer №2

Your method of cloning the student_list is incorrect. Consider this alternative approach.

observe: {
    this.student_list_data = [... this.student_list];
}

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

Running a node.js project on the client side without using npm for deployment

Looking for a solution to efficiently deploy my nodejs project with frequent updates. The site does not have npm available, so I have to package the node_modules every time, which results in a large file size (80MB) that takes a long time to send via ftp t ...

What causes Angular to consistently redirect to the homepage?

Whenever I attempt to access the '/' route, it consistently displays the static-root-component (the main page component). However, if I try to access the '/welcome' route, it immediately redirects back to '/' and loads the sta ...

What is the best way to ensure that an mp3 file plays seamlessly across all pages?

Does anyone know how to ensure an mp3 file plays continuously across all pages using webform asp.net? If you have any solutions, please let me know. jQuery('#main .jp-jplayer').each(function(index) { var i = index+1; var ...

What is the best way to make the children of a parent div focusable without including the grandchildren divs in the focus?

I want to ensure that only the children of the main div are able to receive focus, not the grandchildren. Here is an example: <div class="parent" > <div class="child1" > <!-- should be focused--> <div class="g ...

What steps are involved in connecting FourSquare Venues with the Angucomplete library in Angular?

I'm endeavoring to incorporate Foursquare venues into my Angular application using the angular angucomplete. However, the datafield in angucomplete necessitates an array of objects, while the response from Foursquare is returned as an array of arrays ...

Add a button feature to both upload and download a todo list within a JavaScript file, similar to a text file

I'm looking to enhance my simple todo list with a download and upload function. I want to be able to save the list in local storage and then upload or download it from a server code. Below is a link to my code: https://jsfiddle.net/zahrashokri/uqkn6t ...

Retrieving a specific attribute pair from a JSON object

Currently, I am trying to retrieve the temperature data and display it on my webpage. Since these are objects with no specific order, I am struggling to understand how to access them without using an index. { "response": { "version": "0.1", "termsofServic ...

Can you explain the purpose of using href=javascript:{}?

Can you explain the purpose behind including javascript:{} in the code snippet below? How does it compare to using href="javascript:" or href="javascript:void(0)"? <a href="javascript:{}">This is just an example</a> ...

Combining Two JSON Arrays Featuring Unique Keys

I have two JSON arrays with slightly different keys. The first JSON array contains the keys id, name, and title. The second JSON array includes id, title, forename, name, and company. I am wondering if it's possible to merge these two arrays so th ...

Launching a React project using the terminal - my step-by-step process

I'm attempting to initiate a new react-app utilizing the command npx create-react-app <app name> as shown below: npx create-react-app new-app However, after downloading, it seems to be stuck. It's not progressing at all. I've even rei ...

What strategies can I implement to prevent security alerts in Internet Explorer 8 when accessing Google Maps via a secure connection

When using Google Maps on my project through an https connection, I encountered a problem. Interestingly, the issue only arises in certain browsers while others work fine. Despite searching for a solution extensively, I have not been able to find one. Some ...

Scanner (IE5) impact on page load speeds

I have developed an MVC project which is designed as a single-page application (SPA) utilizing knockoutjs and sammy frameworks. The following script is placed in the head section of the page: <script> var startTime = (new Date()).getTime(); </ ...

Ways to pause YouTube video when hiding a Div

Although I've asked a similar question previously, I am revisiting the issue as I have since taken a different approach. My previous attempts with the YouTube API were fruitless, so I am exploring new options. My goal is to create a website where var ...

Struggling to dynamically update the URL within a "a href" tag, JavaScript is failing to reflect the changes in the output

Currently, I am attempting to use JavaScript to replace the URLs in the HTML. However, I seem to be encountering an issue where the new URLs do not apply when clicking on the images, even though testing with "alert" shows that the values are correct. Any a ...

Vue.js watcher fails to observe changes in v-model

I'm encountering an issue with vue.js. I have set it up so that when a new item is added, it is saved to local storage. However, I also want the item to be saved to local storage when editing it in the input field. I thought this should work because o ...

Changing image using setInterval() with class in p5.js

I'm currently tackling a project involving p5.js. My objective is to rotate images every X seconds, similar to how it was achieved in this example here. Nevertheless, I encountered an issue when attempting the same with a class object. The setInterv ...

Revoke the prior invocation of the Google Maps geocoding function

While working on implementing an autocomplete with JavaScript and the Google Maps geocode method (using the keyup event on input), I have encountered a problem where I sometimes receive the results of the previous search instead of the current one. I am l ...

Unveiling the steps to automatically conceal a submitted form in React, no longer requiring the manual activation of a toggle button

Currently, I have a list of songs and a toggle button that reveals a form to add a new song. However, I want the form to hide automatically after submission without requiring another button click. I tried using useEffect but couldn't get it to work. A ...

Display dropdown with multiple lists using LocalStorage

I'm facing an issue with displaying a list in a dropdown multiple using LocalStorage. The problem arises after saving data to LocalStorage and then going back to my modal. The saved list does not show up in the dropdown. I simply want to display the l ...

Dynamically getting HTML and appending it to the body in AngularJS with MVC, allows for seamless binding to a

As someone transitioning from a jQuery background to learning AngularJS, I am facing challenges with what should be simple tasks. The particular issue I am struggling with involves dynamically adding HTML and binding it to a controller in a way that suits ...