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

404
Views
How to remove dynamic key from useState object in React?

How can I remove dynamic key and it's value from react useState object?

I have a checkedItems object. Based on the function call I want to remove the dynamic key and it's value from the object:

  const [checkedItems, setCheckedItems] = useState(null)

    const unCheckItem =(item) => {
    const {id} = item
    setCheckedItems(prevState) => ({
    ...prevState
    // How should I remove that dynamic key which has the value of id?
    })
    }

How can I remove the dynamic key which has the value of id and also remove it's value?

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

0

You can create helper to remove key and use it on previous state, like this:

const removeKey = (key, { [key]: _, ...rest }) => rest;
...
...
setCheckedItems(prev => removeKey(id, prev));
about 4 years ago · Juan Pablo Isaza Report

0

you can use this way, it will work 100%!

    let newObject = {};
    for (let [key, value] of Object.entries(checkedItems)) {
        if (key != id) {
            newObject[key] = value;
        }
    }
    setCheckedItems(newObject);
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!