Using Vue.js to Delete an Element from an Array

I am facing an issue with my form value where the IDs are not getting deleted when I remove data. Each time I save data, a new ID is added to the list in the form value. But when I delete any of those data entries, the corresponding ID is not removed from the form value.

data() {
  return {
    savedVariations: [],
    form: {
      variations: [],
    }
  }
},
methods: {
  addVariants(e) {
    e.preventDefault();
    axios.post('/api/admin/variations/store', {
      childs: this.variationChilds,
      parent: this.variationParents,
    })
    .then(res => {
      this.form.variations.push(res.data.data.id); // send id to form.variations
    })
  },
  removeSavedParent(id, index){
    axios.delete('/api/admin/variations/destroy/'+id).then((res) => {
      this.form.variations.splice(id); // delete the id from form.variations (not working)
      this.savedVariations.splice(index, 1);
    })
  },
}

After saving new items, my form.variations looks like variations["1","50", "30"]. However, when I delete any of these items, the respective ID is not removed from form.variations.

Does anyone have an idea on how to properly remove an ID from the form variable when deleting data?

Answer №1

Locate the position of the specified id within your array and then eliminate it by implementing the following code snippet:

this.form.variations.splice(this.form.variations.indexOf(id), 1);

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

Exploring the World of 2D Array Animation

I'm in the process of creating a Pacman ghost using visual 2D arrays. I would like the ghost to move similar to this: https://i.sstatic.net/KPmUt.gif I am considering implementing CSS transitions for the movement, but I'm unsure about how to do ...

Access the serialized form data fields using Express.js

I'm currently facing difficulty in accessing specific fields of my serialized formdata within my express router. Here is the ajax request I am using: var formData = $("#add-fut-account-form").find("select, textarea, input").serialize(); $.ajax({ u ...

Tips for resolving rendering page issues in an express app

My application is a straightforward blog platform that showcases a schema for the title, entry, and date of each blog post. There is also an edit/delete feature that is currently under development. When attempting to use the edit/delete button on a selecte ...

Unable to import local npm package due to an error

We are in the process of migrating multiple websites, each with its own project, to Vue.js. As we transfer files over and bundle them using Webpack, we have encountered a need to consolidate similar components and core JavaScript files into a shared librar ...

The Android WebView is unable to run JavaScript code in a local HTML file

Currently, I am attempting to load a local HTML file from the assets folder into a webview. Even though I can successfully load the HTML file in the webview, there seems to be an issue with the file's reliance on an external .js file for calculations. ...

Tips for Sending an Ajax POST Request

I've been using the following code snippet to initiate a POST request to my node API in order to generate a PDF. However, upon execution, my node console displays the following errors: $('#renderPDF').click(function(){ var request = $ ...

Filtering Tables with AngularJS

Currently, I'm experimenting with using angularJS to filter data in a table. My goal is to load the data from a JSON file that has a structure like this (example file): [{name: "Moroni", age: 50}, {name: "Tiancum", age: 43}, { ...

Top method for handling chained ajax requests with jQuery

I'm facing a scenario where I have to make 5 ajax calls. The second call should only be made once the first call returns a response, the third call after the second completes, and so on for the fourth and fifth calls. There are two approaches that I ...

Vue.js not responding to "mousedown.left" event listener

I am trying to assign different functionalities to right and left click on my custom element. Within the original code, I have set up event listeners for mouse clicks in the element file: container.addEventListener("mousedown", startDrag); conta ...

How to iterate through a Vue data object using forEach loop

I am currently working with a variable in my data: data: function () { return { myVariable: false, } } I'm trying to figure out how to access this variable within a looping function, like the example below: anArray.forEach(functi ...

What is the best way to choose all checkboxes identified by a two-dimensional array?

I need help with a question div setup that looks like this: <div class="Q"> <div id="Q1"><span>1. </span>Which of the following have the same meaning?</div> <div class="A"><input type="checkbox" id="Q1A1Correct" /& ...

Member not found error with JQuery Autocomplete on browsers older than Internet Explorer 10

While constructing a web page with JQuery, I encountered issues with my autocomplete feature when testing it on IE8. The error message reads: SCRIPT3: Member not found. jquery-1.6.4.min.js, line 2 character 29472 After extensive research, I have been u ...

The style of MUI Cards is not displaying properly

I've imported the Card component from MUI, but it seems to lack any styling. import * as React from "react"; import Box from "@mui/material/Box"; import Card from "@mui/material/Card"; import CardActions from "@mui/m ...

Monitor Socket IO for client disconnection events

I am facing an issue where I need to identify when a user loses connection to the socket. It seems that socket.on("disconnect") is not triggering when I simply close my laptop, leading to the ajax call not executing to update the database and mark the us ...

The error message "jsPDF is not defined in Laravel using Vuejs and bootstrap-table-vue"

I encountered an issue when attempting to export a Bootstrap table in Vue as a PDF format. The error message I received was: app.js:100649 Uncaught ReferenceError: jsPDF is not defined at jQuery.fn.init../node_modules/tableexport.jquery.plugin/tableEx ...

Show different JSON data based on the existence of another key in Javascript

Having recently started learning JavaScript, I attempted the code below but couldn't quite get it to work. Despite consulting various resources, I still wasn't successful. Desired Output: To check if AUTO damage is present in the data. If so, re ...

Measuring the variable size of an array containing objects of a given class

Recently, I created a basic code/userscript to receive notifications about any changes on a specific website: function notifier(){ setTimeout(function () { location.reload(true); },60000) } function notiCounter() { console.log("Cou ...

Looking to display the precise information from an opened Accordion in a modal window for updating purposes with Django

My main goal is to update data using a modal toggle button within a bootstrap accordion. Each question is retrieved from views.py and displayed within an accordion element. The ideal scenario is for each accordion to have a modal toggle button that, when c ...

Using PHP to globally access a JavaScript object named

I have a collection of CSS attributes stored in a MySQL database that are accessed using PHP. These attributes need to be accessible to JavaScript once the page has finished loading. To achieve this, I loop through each row and create a JavaScript object ...

Showing and hiding elements inside a loop with AngularJS using ng-if and ng

While presenting a separate div based on a condition inside ng-repeat, I encountered an error message that reads "Syntax Error: Token '<' not a primary expression at column 32 of the expression [widget.Type == 'Bar'>". How can thi ...