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

157
Vistas
How to loop through an object and then use array methods on its keys?

I see that all the loops for objects returns the key as string and the value, but I want to operate on the keys of the object itself. If I have this object:

const data = {
   person1: [
      { id: 1, name: Mike, age: 24 },
      { id: 2, name: Bob, age: 31 }
   ],
   person2: [
      { id: 3, name: Christin, age: 21 },
      { id: 4, name: Michelle, age: 33 }
   ],
}

const removePersonById = (id) => {
   // Check which person the id belongs to and remove that person
   const persons = Object.keys(data).map(person => ...)
}

I wanted to loop through data and run .includes on each person in order to remove them by the id, but I am at a loss on how to do that.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can loop through all keys and delete that the person you want by id using the filter() method

const removePersonById = (id) => {
 var all = Object.keys(data);
 for(let person of all){
   data[person] = data[person].filter(a => a.id!=id);
 }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Object.entries() so you can iterate over the keys and values together. Then you can test the value to see if the id is found.

Then use the delete operator to remove that key from the object.

const removePersonById = id => {
    delete data[id];
};

const data = {
   person1: [
      { id: 1, name: "Mike", age: 24 },
      { id: 2, name: "Bob", age: 31 }
   ],
   person2: [
      { id: 3, name: "Christin", age: 21 },
      { id: 4, name: "Michelle", age: 33 }
   ],
};

const removePersonById = (id) =>
  Object.entries(data).forEach(([key, value]) => {
    if (value.some(({id: personid}) => personid == id)) {
      delete data[key];
    }
  });

removePersonById(3);
console.log(data);

about 4 years ago · Juan Pablo Isaza Denunciar

0

let data = {
   person1: [
      { id: 1, name: "Mike", age: 24 },
      { id: 2, name: "Bob", age: 31 }
   ],
   person2: [
      { id: 3, name: "Christin", age: 21 },
      { id: 4, name: "Michelle", age: 33 }
   ],
}

const removePersonById = (id) => {
   // Check which person the id belongs to and remove that person
   data = Object.keys(data).reduce((acc,key) => {
   if(data[key].some(person=>person.id===id)) return acc
   acc[key]= data[key]
   return acc
   }, {})
   console.log(`Remove person with ID ${id}: `,data)
}

removePersonById(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