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

145
Views
array variable to also update when an element is deleted in ReactJS

I have the following code below

const myID = [] //global variable

const tableContents = () = {

const [selectedOptions, setSelectedOptions] = useState([])
const doChange = (selectedOptions) => {
    setSelectedOptions([...selectedOptions])

    for (var i = 0; i < Object.keys(selectedOptions).length; i++) {
      myID[i] = selectedOptions[i].id
    }
  }
}
console.log(myID)

The function doChange is triggered by onChange of a component (Filter button) that has options. Each of the options has an id. What I initially wanted to do is put the id's of the chosen options in an array so that I can pass it as a value of a query. The problem is when I choose multiple options and remove one, the array that displays still includes the id of the deleted option.

For example: Chosen options are option1, option2, option3. When console.log(myID) is executed, 1,2,3 would appear. When I remove option3, the same 1,2,3 would still appear. How do I structure the code wherein when I remove an option, the array displayed will also be updated?

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