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

85
Visualizações
How to filter object with multiple optionnal conditions?

I have this array of objects:

[
 {name: "John", surname: "Doe", car: "BWM"}, 
 {name: "Louis", surname: "Vuitton", car: "MERCEDES"}, 
 {name: "Bob", surname: "Ross", car: "FORD"}, 
 {name: "Dylan", surname: "James", car: "FERRARI"},
 {name: "Damien", surname: "Rivers", car: "JAGUAR"},
]

And I have this code

return this._object.filter(object => object.car === params1 && object.car === params2  && object.car=== params3)

params can be what ever brand car, but they are optional besides params1, user can provide only params1 and let the other two undefined.

Params are selected by the user on a select html option, he can choose up to 3 car brand, so 3 params and i pass it as url parameters and pass these parameters to my function

I want to do something like in one line : if params2 and params3 exists don't change the code snippet, if they don't exist, only do the filter on params1

Example 1: if params1 = BMW and params 2-3 = undefined it would return this object {name: "John", surname: "Doe", car: "BWM"}

Example 2: if params1 = BMW, params2 = MERCEDES it would return this result :

{name: "John", surname: "Doe", car: "BWM"}
{name: "Louis", surname: "Vuitton", car: "MERCEDES"},`

Same concept with params 3 == FERRARI

I can't see how I can do that with few lines/one line, besides doing if statement everywhere

Thanks !

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can do something like this

const filterData = (data, ...params) => data.filter(d => params.includes(d.car))



const data = [{name: "John", surname: "Doe", car: "BMW"}, 
 {name: "Louis", surname: "Vuitton", car: "MERCEDES"}, 
 {name: "Bob", surname: "Ross", car: "FORD"}, 
 {name: "Dylan", surname: "James", car: "FERRARI"},
 {name: "Damien", surname: "Rivers", car: "JAGUAR"},]
 
 
 console.log(filterData(data, 'BMW'))
 console.log(filterData(data, 'BMW', 'MERCEDES'))
 

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make the code even more flexible. Instead of passing 3 different params, pass an array like below.

let cars = ["BMW", "MERCEDES"];

now you can check it using javascript's includes method.

return this._object.filter(object => cars.includes(object.car))
about 4 years ago · Juan Pablo Isaza Relatório

0

By using Array.prototype.includes() you can check against a number of target expressions. It will return true if the argument is equal to at least one element of the array.

In an initial args.map() loop I make sure that the input values are all upper case.

const data=[{name: "John", surname: "Doe", car: "BMW"}, 
 {name: "Louis", surname: "Vuitton", car: "MERCEDES"}, 
 {name: "Bob", surname: "Ross", car: "FORD"}, 
 {name: "Dylan", surname: "James", car: "FERRARI"},
 {name: "Damien", surname: "Rivers", car: "JAGUAR"}];

function filt(...args){
 args=args.map(a=>a.toUpperCase());
 return data.filter(d=>
  args.includes(d.car))
}

console.log(filt("BMW","Mercedes"));
console.log(filt("jaguar"))

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