Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!