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

482
Vistas
Use && operator with filter() method in JavaScript

I have a mental block that does not allow me to move forward.

I have an array that I will exemplify with the following code:

let cars = [
  { brand: "Ford", year: "2012", color: "White", doors: "5", model: "One" },
  { brand: "Chevrolet", year: "2021", color: "Red", doors: "5", model: "Two" },
  { brand: "Chevrolet", year: "2000", color: "Black", doors: "5", model: "Three" },
  { brand: "Citroen", year: "2004", color: "Pink", doors: "3", model: "Four" },
];

I need to store in a variable all those cars that meet the condition of having 5 doors. Well,

let carsWithFiveDoors = cars.filter(({ doors }) => doors == "5");

But I also need it to meet two conditions at the same time. To be more clear, to my new array that has only those 5-door cars, I need to apply another filter that allows me to have only those cars that are not Chevrolet branded, nor red. The problem arises when, by simple logic, I apply this method to my array:

carsWithFiveDoors.filter(({ brand, color }) => brand !== "Chevrolet" && color !== "Red");

The result of this filter is the following array:

let newArrayOfCars = [
  {
    brand: "Ford",
    year: "2012",
    color: "White",
    doors: "5",
    model: "One",
  },
  {
    brand: "Toyota",
    year: "2000",
    color: "Black",
    doors: "5",
    model: "Three",
  },
  {
    brand: "Citroen",
    year: "2004",
    color: "Pink",
    doors: "3",
    model: "Four",
  },
];

With that method what I achieved was to generate an array without any Chevrolet vehicles, but I need to filter out those cars that, at the same time, are red and Chevrolet branded.

How could I achieve this? I've given it a lot of thought and I think I've already burned out.

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

0

You should be using a double filter here in this case. Use the below code and this should solve your ask.

let newArrayOfCars = cars.filter(({ doors }) => doors == "5").filter(({ brand, color }) => brand !== "Chevrolet" && color !== "Red");

Also note that you can try putting all the three conditions at once in a single filter using &&.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since you need to filter out cars that are both brand Chevrolet and red, you need to implement the following logical expression:

let newArrayOfCars = carsWithFiveDoors.filter(({ brand, color }) => !(brand === "Chevrolet" && color === "Red"));
about 4 years ago · Juan Pablo Isaza Denunciar

0

filter will always return a new array - you can't filter on an array in place. So you will need a new filter on the carsWithFiveDoors array to produce a new array of noRedFiveDoorChevs.

Note: the actual output will not result in your expected output.

const cars = [
  { brand: "Ford", year: "2012", color: "White", doors: "5", model: "One" },
  { brand: "Chevrolet", year: "2021", color: "Red", doors: "5", model: "Two" },
  { brand: "Chevrolet", year: "2000", color: "Black", doors: "5", model: "Three" },
  { brand: "Citroen", year: "2004", color: "Pink", doors: "3", model: "Four" },
];

const carsWithFiveDoors = cars.filter(({ doors }) => doors === "5");

const noRedFiveDoorChevs = carsWithFiveDoors.filter(({ brand, color }) => {
  return brand !== "Chevrolet" && color !== "Red"
});

console.log(noRedFiveDoorChevs);

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