Is VueJS2 using the computed property/method to modify Vue data?

My form in Vue is almost complete, but I'm facing an issue with an array that seems to be affecting my Vue.data object directly. Here's the situation:

In my code, I have a method called getParams() which returns an object containing all the data I need to submit. Another method, sanitizeParams(), processes this data and makes some changes before submitting it through AJAX.

The problem arises when dealing with a checkbox selection for bioinformatics, which includes an option for "Other". When this option is selected, it opens an input box for the user to provide custom text. My goal is to remove 'other' from the params.data.bioinformatics array and replace it with Vue.data.bioinformaticsOther.

However, what I've noticed is that when params.bioinformatics.splice is executed, both arrays—this.bioinformatics and params.bioinformatics—are being modified simultaneously. This unexpected behavior is causing issues with my UI because the value stored by Vue no longer matches the intended 'other' option.

I initially had these functions as computed properties, but switched them to methods to troubleshoot, yet the problem persists (hence seeking help). I'm relatively new to Vue and JavaScript, so apologies if this is a basic mistake. The confusion has left me stumped for quite some time now, especially considering other non-array options work flawlessly using the same approach.

If needed, I can provide more details to clarify the scenario further.

EDIT: I have added console.log statements to the code for better clarity and corrected a variable name mentioned in the description.

Answer №1

It seems that the issue lies in the getParams function. The current implementation of getParams is causing the bioinformatics data to be passed by reference. This means that both params.bioinformatics and data.bioinformatics are pointing to the same array. Consequently, any modifications made to one will reflect in the other, leading to the unexpected behavior you are experiencing. To resolve this, consider returning a copy of the bioinformatics property instead of a reference.

getParams: function () {
  return {
    organism:           this.organism,
    species_id:         this.speciesID,
    bioinformatics:     this.bioinformatics.slice(),
    // ... more params
  }
}

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

Switching between sockets on a rotational basis

Imagine there is a division in the HTML file, and an interesting game is being played where each player has the opportunity to change the color. However, there’s a twist – they can only switch colors after the other player has taken their turn. The pla ...

Tips for adding multiple elements to a NumPy array

I have a JSON file that may contain up to 10 nested dictionaries within a list. These nested dictionaries are all related to a single service request number, but each dictionary represents a different request. For example, service request 1 at address one ...

Backbone.js: Navigating the Default Path Issue

I've embarked on creating my very first BB app. Progress is decent, but I've hit a roadblock. My router implementation appears as follows: var PlayersAppRouter = Backbone.Router.extend({ routes: { '': 'index', ...

When implementing Firebase Cloud Messaging with React, the token generated by firebase.messaging().getToken() will vary with every refresh

I'm working on a React web app using Gatsby and I want to integrate push notifications through FCM. My firebase-messaging-sw.js service worker is set up, and I'm trying to retrieve a token using the following method in my app: messaging .req ...

Tips for concealing labels until the submit button is activated

I am currently handling a project involving a Spring Controller and JSP. Within a JSP page, I have included a button labeled Process. Upon clicking this button, two radio buttons are displayed below it along with a Submit button. After tapping the Submit ...

Generate a list of JS and CSS paths for UglifyJS/UglifyCSS by parsing an HTML file

Seeking a tool that can scan an HTML file (specifically a Smarty template file) to extract paths from <script></script> and <link/> tags for use with UglifyJS/UglifyCSS or similar minification tools. Extra credit if it can handle download ...

Is it feasible to utilize math.max with an array of objects?

When it comes to finding the largest number in an array, solutions like this are commonly used: var arr = [1, 2, 3]; var max = Math.max(...arr); But how can we achieve a similar result for an array of objects, each containing a 'number' field? ...

Mapping Form Fields (with Formik)

Currently, the Formik/Yup validation setup in my form is working perfectly: export default function AddUserPage() { const [firstName, setFirstName] = useState(""); const [email, setEmail] = useState(""); return ( <div> <Formik ...

Troubleshooting Issue with Search Functionality on MongoDB Integrated with Next JS

I'm currently working on developing a search system for my Next JS API. This API contains data such as fuel prices and fuel station names. Below is the snippet of code that I am using: class ApiFeatures { constructor(query, queryStr) { this.quer ...

Is there a way to ensure that the selected value remains displayed on the select menu even after

Here is a sample code for a status select input: <label>Status:</label> <v-select :options="options" v-model="status" @input="changeValueStatus" /> data() { return { options: [ { label: 'Su ...

Difficulty integrating Bootstrap with JavaScript file in Visual Studio Code

I am attempting to incorporate a dynamic progress bar by utilizing Bootstrap's progressbar and creating a custom JavaScript file. The goal is to continuously adjust the width of the progress bar at regular intervals to give the appearance of a moving ...

Unable to view Chart.js on the second tab

I'm currently working on two different charts for a project - a bar chart and a line chart. The bar chart is displayed on the first tab, while the line chart is on the second tab. Interestingly, the bar chart functions properly, and when I point the l ...

Stop all animations in JS and CSS

Looking for a way to halt all CSS and JavaScript animations, including canvas and webGL effects, on a specific webpage. Some animations can cause slow performance on certain browsers like Opera and Firefox, so I'm seeking a code snippet or guidance o ...

Issue encountered while attempting to include a background image in React JS

I encountered an issue when attempting to include a background image in react js. ./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css) Unable to locate module: An attem ...

Why am I getting the error "TypeError: Object(...) is not a function" when attempting to integrate Vanilla Tilt with React?

Looking for assistance on implementing Vanilla Tilt in my React application, I have referenced the following example: https://codesandbox.io/s/vanilla-tilt-with-react-n5ptm The code snippet I am working with is as follows: import React, { Component, useEf ...

Is Cognito redirect causing issues with Angular router responsiveness?

When employing social login via AWS Cognito, Cognito sends a redirect to the browser directing it to the signin redirect URL after signing in. In this case, the specified URL is http://localhost:4200/home/. Upon receiving this redirect, the application in ...

Having trouble accessing an injector service within the promise of a dynamically loaded JavaScript function that has been assigned to a global variable

Query I am facing an issue while trying to integrate PayPal with Angular. I am encountering difficulties when attempting to call an injected service inside a function of the promise returned. Any assistance in resolving this would be greatly appreciated. ...

Leveraging Amplify for offline storage while transitioning between HTTP and HTTPS protocols

Recently, I encountered an issue with Amplify 1.0a1 on my website. It seems that when storing the value of prod_id in localStorage on a page using HTTP, and then navigating to a page using HTTPS (such as the 'list' page), I am unable to access th ...

Submitting an AJAX request to upload an XML file and an image file simultaneously in one call

I need to send both an uploaded image and an uploaded XML file to a PHP file using a single AJAX call. I have tried using two form data instances, but I am not sure if this is the correct approach. <input type="file" class="form-control-file" name="fil ...

The clash between two jQuery plugins featuring identical function names

I have encountered a dilemma while working on a large website that includes two conflicting jQuery plugins for autocomplete functionality. The first plugin is jquery.autocomplete.js (not part of jQuery UI) which defines the autocomplete function like this ...