Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda