0

I have a vue object with all these getters and setters, here is a screenshot from the console.log:

Image

The structure of the actual DATA (the not-vue stuff) looks like this:

       {
          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"
          }
        }

The data might look different in other usecases. There might be more or less key-value pairs on the outer object, and the keys might be named differently as well. Same goes for the nested objects and their contents.

Is there some convenient way to extract this data into a plain JS Object? If not, how can I best loop the vue object to extract the data "manually"? The AJAX request shall be performed by an axios request, if this is important as well.

EDIT: Here is the relevant data in vue:

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

During my programflow, both editingRecord.orginal and editingRecord.copy receive data from an inputform. copy sets its data to original if the user clicks the save/send button. Then, I want to take the data from editingRecord.original with both its keys and values and send them to the server via AJAX request.

6
  • There's no clear link on the data in your VueJS and the data you want to send to the server. How do you want to transform the data? It's not clear in your question at all. Commented Mar 19, 2020 at 10:17
  • @Terry What do you mean by Link? Commented Mar 19, 2020 at 10:19
  • You didn't show any data in your VueJS app: so how are we going to know how you want to transform the data to the shape you want in the POST request? Commented Mar 19, 2020 at 10:21
  • @Terry See my edit I hope this is what you wanted to see. Commented Mar 19, 2020 at 10:29
  • What happens if you make a post request with this.editingRecord.original as the body? Commented Mar 19, 2020 at 11:33

2 Answers 2

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)
      });

    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.