Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
Problem with deleting array which containts other arrays with specific id item in React

I have Array which containts a lots of arrays which containts {id and content}.

Example : [Array(1), Array(1), Array(1)].

Im trying to delete Array with ID param. It deletes but state of Array.Length doesnt change because i only deleted Item of array ,not whole of one array.

What i want to do is delete () -> than result should be

[[Array(1), Array(1)]

What i got? [Array(0), Array(1), Array(1)]

There is code what im tried to do :

return (
   <div>
      <h1>{tasks.length}</h1>
         {tasks.map((task) => {
            return (<div>
               {task.map((con) => {
                  return (<div>
                     <h3 className="hrandom">{con.content}
                     <button type="button" class="btn btn-danger" onClick={() => { deleted(con.id) }}>Delete</button></h3>
                  </div>
               )
            })}
         </div>)
      })
   }
</div>)

And the delete function :

const deleted = (id) => {
   Axios.delete('http://localhost:3001/api/delete/', { data: { id: id } }).then((res) => {
      console.log(res.data)
   });
   const b = tasks.map((tod) => tod.filter((i) => i.id !== id ));
   setTasks(b);
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In your map, you're removing the items from the inner array but not the outer array itself. You could do this in a single line, but for simplicity and readability, add a second filter to the outer array:

const b = tasks.map((tod) => tod.filter((i) => i.id !== id )).filter(arr => arr.length > 0);

This will remove any inner arrays that have been emptied, in a very readable manner. However, noting the inner array always has length 1, you could destructure cleanly like so:

const b = tasks.filter(([{ id: inner }]) => inner !== id);

The destructure syntax takes the id property of the object that is the first element of the inner array, and compares it to id.

about 4 years ago · Juan Pablo Isaza Denunciar

0

For your example; instead of using a filter inside a map function - which still returns an array of the same length, you should use the filter function to filter out the parent array directly:

const b = tasks.filter((tod) => tod.findIndex((i) => i.id === id ) < 0);

However, if there is only one array entry that will contain that id, filter may not be the greatest choice as it will loop across the entire tasks array even if the id has already been found. In such case, I would recommend first finding the index of the element that contains an array with the id and using array.splice to remove it from the tasks array as follows:

const index = tasks.findIndex((tod) => tod.findIndex((i) => i.id === id ) > -1);
if (index > 0) {
  tasks.splice(index, 1);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda