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

359
Visualizações
How to reference to element from state in foreach (Vuex)?

I am using vuex in my vue application. In store is a declared object:

    state: {
        list: {
            a: false,
            b: false,
            c: false
        }
    }

In mutations is mutation that receives arrays in parameters, for example: el: ['a', 'b']. Those elements in the el array must be set to true in list object in state. I'm using a foreach loop for this:

    mutations: {
        SET_LIST(state, el) {
            el.forEach(element => {
                if (state.list.element) {
                    state.list.element = true;
                }
            });
        }
    }

But I am getting an error: error 'element' is defined but never used. Because element is not used and I don't know how to refer to it correctly.

I searched on the internet and found this solution: state.list[element]. I don't get an error then, but it doesn't work.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use the bracket notation [] to get the property dynamically :

   mutations: {
        SET_LIST(state, el) {
            el.forEach(element => {
                if (Object.keys(state.list).includes(element)) {
                    state.list[element] = true;
                }
            });
        }
    }
over 4 years ago · Santiago Trujillo 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