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

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

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 Relatório

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 Relatório

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