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

479
Views
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 answers
Answer question

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 Report

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 Report

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