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

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