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

276
Vistas
A way to conditionally chain functions?

What im trying to do is to write a universal function for retrieving items from database using redis-om-node. It uses this chain of methods to specify what items we want to retrieve

const items = await repositoryObject
.search()
.where("property1")
.match(query)
.or("property2")
.match(query)
.or("property3")
.match(query)
.return.all();

Is there a way I could loop through array of properties to make this chain write itself like:

const items = await repositoryObject
.search()[loop outputed chain].return.all();

Im aware that I can just run raw search but I want to know am I missing something about javascript objects

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

0

Sure, you could create an array of objects which contain the properties and corresponding queries that you want to use, and then loop through it like this:

const queries = [{ property: "property1", query: query1 }, { property: "property2", query: query2 }, { property: "property3", query: query3 }]; // etc

let result = repositoryObject.search();

for (const { property, query } of queries) {
  result = result.where(property).match(query);
}

return result.return.all();

You could get more fancy and use reduce if you wanted to:

// queries is as above

return queries.reduce((object, { property, query }) => object.property(property).match(query), repositoryObject.search()).return.all();

or use the forEach method on the array to loop. Basically you can loop all the ways you have in normal Javascript - you just have to use it to construct a property chain of the form you showed us.

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