Dran Dev
Dran Dev

Reputation: 519

For loop for VueJS?

I tried this:

get_choices: function (item, index){
    console.log(item)
},

this.questions.forEach(this.get_choices())

But it says

Error in mounted hook: "TypeError: this.questions.forEach is not a function"

I also tried:

for(question in this.questions){
    console.log(question)
}

But it didn't output anything, it didn't also displayed any error. Any help? Thanks a lot!

UPDATE:

Here's the this.questions. It is an array of objects:

data: {
    questions: [
        {'name': 'etc', 'content': 'etc'},
        {'name': 'etc2', 'content': 'etc2'},
    ],
},

Upvotes: 0

Views: 1308

Answers (2)

Anis
Anis

Reputation: 1219

you need to pass the object to function as

this.questions.forEach((item , index) =>{
      this.get_choices(item , index)
 })

or you can also do like

this.questions.forEach(this.get_choices)

Upvotes: 0

Ilijanovic
Ilijanovic

Reputation: 14914

You need to remove the paranthesis from your function call, you just need the reference

this.questions.forEach(this.get_choices)

Upvotes: 2

Related Questions