I'm building a flashcard app for my English/Chinese dictionary in Vue 3 and when the user clicks a button I want the next item to show on the screen from the imported flashcards array.
Without having to iterate through the array with a for loop and pause the iteration until the user clicks a button, I want to simply grab the item in the array by it's index by setting a count -
<script>
import flashcards from '../flashcards/flashcards.json';
export default {
data() {
return {
count: 0,
currentTranslation: {
chinese: flashcards[this.count].traditional,
},
}
},
methods: {
correctClickHandler() {
this.setNextTranslation()
},
incorrectClickHandler() {
this.setNextTranslation()
},
setNextTranslation() {
this.count++
}
}
}
</script>
When one of the click handlers is called, the count should increment by one and then the next item in the flashcards array should show.
Referencing this.count as an array index returns an undefined error -
Uncaught TypeError: Cannot read properties of undefined (reading 'traditional')
Why isn't flashcards[this.count].traditional the same as flashcards[0].traditional when this.count has already been set to 0?