[Vuejs]-How to dynamically extract data from vue object for a POST request to backend?

0👍

Its best not to mix jQuery with Vue so here’s how you would do it using Axios https://github.com/axios/axios

methods: {
    submit() {
      const body = this.editingRecord.original

      // make changes to body

      axios.post( "backend_url", body )
      .then(function(resp){
        console.log(resp)
      });

    }
  }

0👍

Okay, I found the solution.

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

  for(const[key, value] of vueObject){
    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)

This way you can iterate over all properties including the properties of nested objects and reassign both key and value to a fresh, one-dimensional array.

Leave a comment