0

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}]
2
  • 1
    Mention what format of array you want . Commented Aug 11, 2018 at 4:49
  • 1
    this.Answers = Object.values(this.info)? See Object.values() Commented Aug 11, 2018 at 4:53

2 Answers 2

2

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 }) )
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Don't make it a method if you can make it a computed property. Computed properties are cached and automatically updated when the data it depends on changes. There is no reason in this example to use a method.
1

Make a computed property that calculates answers based on info.

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

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.