Pioz
Pioz

Reputation: 25

How do you convert form values into an array in VUE

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}]

Upvotes: 0

Views: 1639

Answers (2)

Sumurai8
Sumurai8

Reputation: 20745

Make a computed property that calculates answers based on info.

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

Upvotes: 1

M Shafique
M Shafique

Reputation: 816

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

Upvotes: 2

Related Questions