Home » vue » How do you convert form values into an array in VUE

How do you convert form values into an array in VUE

Posted by: admin November 26, 2021 Leave a comment

Questions:

I’m trying to figure out how to convert info into an array to compare it with another array.

This is my code

<input type="checkbox" v-model="info.q1">
<input type="checkbox" v-model="info.q2">
<input type="checkbox" v-model="info.q3">

Answers[],    
info : {
      q1: '4',
      q2: '4',
      q3: '4'
    }

I’m trying to get something like the below:

var answers = [{q1: 4},{q2: 4}]
Answers:

You can make a Computed Property with the name answers like:

    answers () {
        return Object.entries(this.info).map( ([key, value]) => ({ [key]: value }) )
    }

After creating computed property you can call it like this.answers.

or

You can make make in method like:

    methodName(){
        const answers = Object.entries(this.info).map( ([key, value]) => ({ [key]: value }) )
    }

###

Make a computed property that calculates answers based on info.

computed: {
  answers () {
    return Object.entries(this.info).map(
      ([key, value]) => { [key]: value }
    )
  }
}