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

114
Vistas
Running .filter and .map synchronously

Hello I am having an issue running this function wherein im trying to filter this an array of objects then afterwards im reorganizing by reassigning my id with the index of the .map The issue that occurs is that the .filter is not deleting/removing an items in the function. Here is my code as reference: What do be the best solution of this. Any input would be helpful thank you.

 const removeData = (id) => {
        const del = idleData.filter(idle => id !== idle.id).map((item,index) => (
            {
                id: index, 
                days: item.day,
                comments: item.comments,
                cost: item.cost,
                reason: item.reason,
                portCallVoyageId: item.portCallVoyageId,
                changedBy: item.changedBy
            }
        ));
        setIdleData(del);
    }

Here is idleData and idle:

idleData = [{
  id: 0,
  cost: 10,
  days: 10,
  comments: 'comment 1',
  reason: "reason 1",
  changedBy: "user1"
}, {
  id: 1,
  cost: 20,
  days: 20,
  comments: 'comment 2',
  reason: "reason 2",
  changedBy: "user2"
}];

idle = 0;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just tested your code and I think it works fine and the possible problem is in the idle variable.

I slightly rewrote the source code.

const idleData = [
{id: 0,cost: 10,days: 10,comments: 'comment 1',reason: "reason 1",changedBy: "user1"}, 
{id: 1,cost: 20,days: 20,comments: 'comment 2',reason: "reason 2",changedBy: "user2"},
{id: 2,cost: 30,days: 30,comments: 'comment 3',reason: "reason 3",changedBy: "user3"},
{id: 3,cost: 40,days: 40,comments: 'comment 4',reason: "reason 4",changedBy: "user4"},
{id: 4,cost: 50,days: 50,comments: 'comment 5',reason: "reason 5",changedBy: "user5"},
];

//idle = 0; 

const removeData = (omitId) => {
  const del = idleData
    .filter(({ id }) => id !== omitId)
    .map((item, index) => ({ ...item, id: index }));

  console.log(del);
  // setIdleData(del);
};

  console.log(del);
  // setIdleData(del);
};

removeData(0);
.as-console-wrapper{min-height: 100%!important; top: 0}

Same result but without .filter

const idleData = [
{id: 0,cost: 10,days: 10,comments: 'comment 1',reason: "reason 1",changedBy: "user1"}, 
{id: 1,cost: 20,days: 20,comments: 'comment 2',reason: "reason 2",changedBy: "user2"},
{id: 2,cost: 30,days: 30,comments: 'comment 3',reason: "reason 3",changedBy: "user3"},
{id: 3,cost: 40,days: 40,comments: 'comment 4',reason: "reason 4",changedBy: "user4"},
{id: 4,cost: 50,days: 50,comments: 'comment 5',reason: "reason 5",changedBy: "user5"},
];

//idle = 0; 

const removeData = (omitId) => {
  const del = idleData
    .flatMap((item, index) => (item.id !== omitId) 
      ? { ...item, id: index } 
      : []);

  console.log(del);
  // setIdleData(del);
};


removeData(0);
.as-console-wrapper{min-height: 100%!important; top: 0}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code is working like a champ! I am adding the code snippet below. Please let me know what challenges you are facing. I will modify that as per the input.

Demo :

const idleData = [{
  id: 0,
  cost: 10,
  days: 10,
  comments: 'comment 1',
  reason: "reason 1",
  changedBy: "user1"
}, {
  id: 1,
  cost: 20,
  days: 20,
  comments: 'comment 2',
  reason: "reason 2",
  changedBy: "user2"
}];

 const removeData = (id) => {
   const del = idleData.filter(idle => id !== idle.id).map((item,index) => (
     {
       id: index, 
       days: item.day,
       comments: item.comments,
       cost: item.cost,
       reason: item.reason,
       portCallVoyageId: item.portCallVoyageId,
       changedBy: item.changedBy
     }
   ));
   console.log(del);
 }
 
 removeData(0);

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