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

114
Visualizações
How to filter collection with multiple conditions (strings and array of values) using lodash javascript

I'm trying to filter a collection with multiple conditions using lodash, it works fine with strings, but I want to filter by an array of id's for the part of the locations and string for status. I have tried to change the id to array of id's "location":{"id" :["1","4"]}, but it doesn't work

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

 let filterByConditions = {
  "location":{"id" :"1"},
  "account":{ "id": "1"},
  "status": "ok",
};

let results = _.filter(collection, filterByConditions);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like you're trying to use the filter method with the "matches" shorthand. This shorthand doesn't support an "or" condition; only exact matches. However, you can pass the _.filter method a predicate instead, and it'll work:

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

let filterByPredicate = (elem) => {
  return elem.status === "ok" && 
    elem.location.id === "1" && 
    (elem.account.id === "1" || elem.account.id === "4");
};

let results = _.filter(collection, filterByPredicate);
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