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

174
Views
What is the easiest way to filter an object in javascript?

Yes, I know, there is already a question about how to filter objects in javascript, but look, I'm not asking how to do it, I'm asking about what would be the easiest way, the one which a noob like me could remember.

Before ask I spent a little time and after some tests I came up with theses solutions. They are examples but I cannot think about something better by now:

function queryHandlerOne(query: any) {
  const fields = ["name", "featured", "price", "company", "rating"];
  
  const entries = Object
    .entries(query)
    .filter(([key, _]) => fields.includes(key));

  return Object.fromEntries(entries);  
}
function queryHandlerTwo(query: any) {
  const fields = ["name", "featured", "price", "company", "rating"];

  const entries = Object
    .keys(query)
    .filter(key => fields.includes(key))
    .reduce((acc, key) => {
      acc[key] = query[key];
      return acc;
    }, {} as any);

  return entries;
}
function queryHandlerThree(query: any) {
  const fields = ["name", "featured", "price", "company", "rating"];

  const result = Object
    .keys(query)
    .filter(key => fields.includes(key))
    .map(key => [key, query[key]] as [string, any]);

  return Object.fromEntries(result);
}
function queryHandlerFour(query: any) {
  const fields = ["name", "featured", "price", "company", "rating"];
  const result = {};

  Object
    .keys(query)
    .filter(key => fields.includes(key))
    .forEach(key => Object.assign(result, { [key]: query[key] }));

  return result;
}
about 4 years ago · Juan Pablo Isaza
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!