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

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

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