Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!