Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
¿Cómo encontrar todas las combinaciones de condiciones if de manera efectiva?

Me gustaría encontrar de manera efectiva, debido al anidamiento de código.

 this.store.carsWithFilters = this.store.cars.filter(car => { if (price && car.price < price.value) { return car } if (tachometer && car.tachometer < tachometer.value) { return car } if (cars.length > 0 && cars.includes(car.name.split(' ')[0])) { return car } if (colors.length > 0 && colors.includes(car.color)) { return car } if (fuel && car.fuel === fuel.text) { return car } })

convertirse en todas las condiciones posibles

por ejemplo tengo 3 condiciones

 1 - if condition1 2 - if codititon2 3 - if condition3 4 - if condition1 && condition2 5 - if condition2 && condition3 6 - if condition1 && condition3 7 - if condition1 && condition2 && condition3
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

polimofismo podría ayudar aquí. Intente usar un marco web que use objetos y clases, e intente usar polimorfismo para deshacerse de estos condicionales. EG Intente usar Typescript y desarrolle un modelo que represente la relación entre objetos para usar polimorfismo, en lugar de condicionales.

about 4 years ago · Santiago Trujillo Report

0

Puedes factorizar el if usando && (y)

 if (price && car.price < price.value) && (tachometer && car.tachometer < tachometer.value) && (cars.length > 0 && cars.includes(car.name.split(' ')[0])) && (colors.length > 0 && colors.includes(car.color)) && (fuel && car.fuel === fuel.text) { return car }

Pequeño ejemplo:

Supongamos que tiene un artículo de automóvil que tiene 3 propiedades:

  • NbAsientos
  • Combustible
  • Color

Ahora quiere que todos los siguientes valores sean verdaderos

  • El coche debe tener al menos 5 plazas.
  • El auto debe tener combustible de plomo 10
  • El coche debe ser azul.

Luego verificará con la siguiente declaración if:

 if(car.nbSeats > 4 && car.fuel > 10 && car.color === "blue")

Si desea que al menos una propiedad sea verdadera (no todas), verificará con || palabra clave

 if(car.nbSeats > 4 || car.fuel > 10 || car.color === "blue") 

 const car1 = { nbSeats: 4, fuel: 20, color: "red" } const car2 = { nbSeats: 7, fuel: 5, color: "blue" } const car3 = { nbSeats: 6, fuel: 20, color: "blue" } function isCarValid(car){ if(car.nbSeats > 4 && car.fuel > 10 && car.color === "blue") return "Valid" else return "Not valid" } console.log(isCarValid(car1)) console.log(isCarValid(car2)) console.log(isCarValid(car3))

about 4 years ago · Santiago Trujillo Report

0

El filtro solo necesita true o false para que pueda devolver solo las declaraciones. Sin embargo, si desea ignorar ciertas variables, debe permitir que undefined/null/0 devuelva verdadero.

 this.store.carsWithFilters = this.store.cars.filter(car => { (!price || car.price < price.value) && (!tachometer || car.tachometer < tachometer.value) && (cars.length == 0 || (cars.includes(car.name.split(' ')[0] || colors.includes(car.color))) && (!fuel || car.fuel === fuel.text) });

Esto se ve muy desordenado, así que usaría variables para hacerlo más limpio. Además, es más fácil depurar si se usan variables.

 this.store.carsWithFilters = this.store.cars.filter(car => { let affordable = !price || car.price < price.value, milage = !tachometer || car.tachometer < tachometer.value, correctCarModel = cars.length == 0 || cars.includes(car.name.split(' ')[0], correctCarColor = cars.length == 0 || colors.includes(car.color), correctFuelType = !fuel || car.fuel === fuel.text; return affordable && milage && (correctCarModel || correctCarColor) && correctFuelType; });

Por lo tanto, si solo tiene fuel como variable, affordable , milage , modelo de coche correctCarmodel y color de coche correctCarColor devolverán verdadero.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!