Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Is there a way to reference an array index dynamically in JavaScript?

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

this is not what you think it is inside data. To be specific, it's not a reference to the current Vue instance, which is true for methods or computed functions. It's the data function's this and therefore none of the instance properties or methods are available on it.

In your case, to retain currentTranslation dependency on this.count, it should be a computed:

<script>
  import flashcards from '../flashcards/flashcards.json';
  export default {
    data() {
      return {
        count: 0
      }
    },
    computed: {
      currentTranslation() {
        return flashcards[this.count].traditional
      }
    },
    methods: {
      correctClickHandler() {
        this.setNextTranslation()
      },
      incorrectClickHandler() {
        this.setNextTranslation()
      },
      setNextTranslation() {
        this.count++
      }
    }
  }
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

in this.data, this.count is probably not yet set at the time when you try to reference it in this.currentTranslation.chinese. What I mean with this is that if you define an object, the property isn't set when you are still in the "creation" phase. Therefore, you can't reference the object itself in that step to reuse properties you defined above your current property.

obj = {
   a: 1,
   b: obj.a + 1
}

This will give you

Uncaught ReferenceError: obj is not defined

You'd have to do this:

obj = {
   a: 1,
}
obj.b = obj.a + 1

In Vue you can use a computed property.

import flashcards from '../flashcards/flashcards.json';
export default {
    data() {
        return {
            count: 0,
        }
    },
    methods: {
        correctClickHandler() {
            this.setNextTranslation()
        },
        incorrectClickHandler() {
            this.setNextTranslation()
        },
        setNextTranslation() {
            this.count++
        }

    },
    computed: {
        // a computed getter
        chineseTranslation() {
            return flashcards[this.count].traditional
        }
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda