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

228
Views
React array map function counter value always coming same

In my react application I am using the array map() function to generate JSX. In that, I am using useState to set the values to an array. I am using a counter to manage the index of the array.

let c= 0
    arr.map((arr2, index) => {
                    c++ 
                    chk_str = arr2.map((value) => {
                    frc_t_id = value.t_id;
                        return <div class="form-check mb-1">
                            <input type="checkbox" class="form-check-input" value={value.domain} onChange={e => {setData(data.splice(c, 0, {domain: e.target.value}))}} />
                            <label class="form-check-label" for="exampleCheck1">{value.domain}</label>
                        </div>
                    })
    
                    return <div className="FormGroup col-md-12">{chk_str}
                        <select name="status" id="" placeholder="Server Status type" class="custom-select" onChange={e => {console.log(c); data[c].server = e.target.value; setData(data)}}>
                            <option value={t_id}>{host_name}</option>
                            <option value={frc_t_id}>{keys[index]}</option>
                        </select></div>
                })

There are two elements in the first array. Here the problem is that in the dropdown onChange event the counter value c is always 2. Please help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use index of map instead of using let c = 0 because let is block scope so it doesn't work well with map function

about 4 years ago · Juan Pablo Isaza 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!