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

114
Views
React remove one item of grouped array useState

hey how can i remove an object from an grouped array ? my grouped array looks like the picture , its grouped by the date and i want to remove one item from it in my function:

  const [inCard, setInCard] = useState(0);
  const [events, setEvents] = useState([]);
  const [eventsInCard, setEventsInCard] = useState([]);

  function addToCard(event) {
    if (!eventsInCard.some((e) => e._id === event._id)) {
      setEventsInCard([...eventsInCard, event]);
      newValueInCard++;
      setEvents((events) => 
            events.filter((item) => 
                item._id === event._id)); //<= not working because its an object 
    }
    setInCard(newValueInCard);
  }

can someone help me ? its not workinggrouped array

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can create a wizard to remove the key and use it in the old state, like so:

 const removeKey = (key, { [key]: _, ...rest }) => rest; ... ... setEvents(prev => removeKey(event._id, prev)); // If event._id is datetime-like key you want to delete, otherwise replace it with appropriate one

UPDATED

If you want to update the array inside the object (under some date-like key), you can do it like this:

 const dateKey = event.somePropFromEvent; // YOU NEED TO KNOW UNDER WHICH KEY IS ARRAY YOU WANT TO MODIFY const id = event._id; setEvents(prev => ({...prev, [dateKey]: prev[dateKey].filter(x => x._id !== id)}));
about 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!