Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda