What is the best way to extract data dynamically from a Vue object in order to make a POST request to the

My Vue object is filled with various getters and setters. Below is a console.log screenshot for reference:

https://i.sstatic.net/dyPvO.jpg

The structure of the raw data (non-Vue related) appears as follows:

       {
          Internal_key: "TESTKEY_1",
          extensiontable_itc: {
            description_itc: "EXTENSION_ITC_1_1",
            description_itc2: "EXTENSION_ITC_1_2",
          },
          extensiontable_sysops: {
            description_sysops: "EXTENSION_SYSOPS_1"
          }
        }

In different scenarios, the data might have varying key-value pairs within the outer object and may also differ in naming conventions for both keys and nested objects.

Is there an effective method to extract this data into a standard JS Object? If not, what would be the best approach to iterate through the Vue object for manual extraction? The AJAX request will be carried out using Axios, if that detail is relevant.

EDIT: Below is the pertinent Vue data:

data() {
  return {
    editingRecord: {
        original: null,
        copy: null
      }
  }
}

Throughout my program flow, data is inputted into both editingRecord.original and editingRecord.copy from a form. When the user clicks save/send, the data from editingRecord.original, including all keys and values, should be sent to the server via an AJAX request.

Answer №1

Avoid mixing jQuery with Vue for better compatibility. Instead, use Axios for handling requests. Visit https://github.com/axios/axios for more information.

methods: {
    sendData() {
      const data = this.updatedRecord.original

      // modify the data as needed

      axios.post( "your_backend_url", data )
      .then(function(response){
        console.log(response)
      });

    }
  }

Answer №2

After some investigation, I have managed to find the solution to the problem at hand.

let obj = Object.entries(this.editingRecord.original)

for(const[key, value] of obj){
  if(typeof value == 'object' && value !== null){
    for(const [nestedKey, nestedValue] of Object.entries(value)){
      result[nestedKey] = nestedValue
    }
  }else{
    result[key] = value
  }
}
console.log("resultObject is", result)

By utilizing this method, it becomes possible to iterate through all properties, even those within nested objects, and subsequently reassign both key and value into a new one-dimensional array.

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

The onLayoutChange function keeps overwriting the data stored in my local storage

When my layout changes, the new changes are saved into local storage and the layout state is updated. onLayoutChange(layouts) { saveToLS("layouts", layouts); } However, an issue arises when the page is refreshed. The layout g ...

When the class name is identical, the click event appears to be malfunctioning

Why isn't the click event working in this code? What am I doing wrong? $(document).ready(function() { $('.dashboardList').click(function() { alert("hello"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1. ...

Using Laravel to transfer multiple data to Vue

This is a section of my .vue file: <script> export default { props: ['photog-Id', 'gal-Id', 'photo-Id'], mounted() { console.log('Component has been mounted.') }, methods: { se ...

Is there a way to combine all the text on an HTML page into one continuous string without losing the CSS styling?

If I want to change all the text within every HTML element on a page to just the letter "A", how would I do it? Let's say I have a webpage set up like this: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

Assign a class to the following element using an Angular 2 Directive

I have a dropdown menu and I want to incorporate an Angular2 directive to control the opening and closing of this dropdown. How can I apply the open class to the latest-notification div, knowing that my directive is applied to the button tag? Below is my ...

Guide on how to dynamically generate a dropdown menu based on the selection made in another dropdown

Could anyone provide me with a demonstration on how to dynamically create a dropdown menu based on the onchange event of another dropdown? I currently have a dropdown menu where users can select multiple options, as shown below: <select id='fir ...

execute task to optimize CSS encoding in the build process

Creating a static project with yeoman -webapp I've implemented a UTF checkmark in my .scss files: [type="checkbox"]:checked + label:after { content: '✔'; position: absolute; top: 3px; left: 0px; font-size: 22px; color:$color-pr ...

execute javascript code after loading ajax content

Although this question may have been asked before, I am completely unfamiliar with the subject and unable to apply the existing answers. Despite following tutorials for every script on my page, I have encountered a problem. Specifically, I have a section w ...

Leveraging Iframes for efficient user authentication within an Angular application

Incorporating an Iframe into one of my templates for authentication has presented certain challenges. Case in point: When a user finishes a training session, they must verify their identity by authenticating with a ping identity server that will redirect ...

Combining v-for with emitted values for optimal functionality

In my application, I have a table that displays a list of countries, as shown in the image below (also, check out the full codesandbox link provided): https://i.sstatic.net/Me8ad.png Whenever the user clicks on the "Review" button, a modal window pops up ...

How can I append an object key to the end of a URL link within an anchor tag

I seem to be facing a problem where I am attempting to append the key of my object at the end of a URL. It appears to work fine as a button, but for some reason the key is not being displayed within the href attribute. Typically, I would use "+" to conca ...

Arranging titles on the top of the page in a column format, resembling an index

Has anyone figured out how to make the title of the content stick at the top, one below the other, as the user scrolls? I've been using Bootstrap's Scrollspy, which works fine, but I need a few additional features. You can check out the codepen l ...

Tips for avoiding accidentally selecting nearby text while holding down a button on your mobile device

My application requires the user to press and hold a button to record audio. However, on mobile devices, when the user holds the button, the browser attempts to select nearby text due to finger pressure. While this behavior is understandable, I want to pre ...

Bug in Click Behavior of HTML5 Canvas on Webkit Browser Versions Below 535 for Android Native and iOS5 Safari

Recently, I encountered a peculiar bug in Android where positioning a canvas element on the Native Android Browser (WebKit 534.4) using css position:absolute; top:10px; left:100px; resulted in the duplicated Canvas being rendered to the browser while ignor ...

When implementing variables from input boxes, my SQL query fails to populate any data in the database table

I have been using phpMyAdmin to store test data. As I try to insert data from a form, I encounter an issue where no data gets inserted when using variables in the SQL query. Being new to coding, I am struggling to find a solution to this problem. Additiona ...

tips for adjusting padding in bootstrap-vue select component

Having trouble customizing the options in a bootstrap-vue select component. I am looking to add padding on top. I have attempted to modify the "custom-select" class and the option tag, but have not found a solution yet. https://i.sstatic.net/KMvEU.png ...

Converting the source to your image assets in Angular: A step-by-step guide

I am looking to update the source code. Here is an example: <div *ngFor="let item of meal.allergenList" class="btn btn-primary"> <img [src]="item" alt=""> </div> I want to make the following co ...

Using Modal Functions in AngularJS Controller

I have been working on a project that utilizes the ui.bootstrap. As per the instructions from the tutorial I followed, my setup looks something like this: 'use strict'; angular.module('academiaUnitateApp') .controller('EntryCtr ...

Creating a list using variables through a Post Request in Express

I am currently exploring how to create a list using a Post Request in Express. I am fetching Video Game data from an API and aiming to use this data to populate specific details within a list. For illustration: let name = localStorage.getItem("name"); let ...

Using React Router useHistory to navigate to different routes programmatically

Currently, I'm working on creating a wizard interface and running into some issues with the buttons. Right now, I have next and back buttons for each route, but I am aiming to create a button component that can handle navigation both forward and backw ...