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

199
Views
Filter and Include don't work together as expected

So I have a clients that I'm iterating, then each client has some vehicles as an array represnted in strings, What I want to do is iterate the clients and check if a specific id is included in any of the .vehicles key, but I'm not getting the wanted results, check my function:

const v = clients.clients.filter((c) => {
  if (c.vehicles.includes(fields.customer) === true) return c;
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You're making it more complicated than needed. You need to return true or false inside filter so:

const v = clients.clients.filter((c) => 
   c.vehicles.includes(fields.customer)
);

Then you have a list of clients that have your values. At last your probably use a map to get an array with the desired results.

I'm guessing you want to have all vehicles so the next line is:

let vehicles = v.map(c => c.vehicles)
about 4 years ago · Juan Pablo Isaza Report

0

An array filter callback should return a boolean, not the item itself

You can try this way

const v = clients.clients.filter((c) => c.vehicles.includes(fields.customer));
about 4 years ago · Juan Pablo Isaza Report

0

You don't need to put a new if condition in your filter. The filter callback function takes the validation condition directly

const result = words.filter(word => word.length > 6) ;

As you can see above (cf mdn) , the filter method will return a new array with only words that have more than 6 letters.

So, in your case, your function should be only

const v = clients.clients.filter(c => c.vehicles.includes(fields.customer)) ;

Also, I think you need to be careful with the key name, here you have a clients key in an object that has the same name, it's not really understandable, maybe you can have carFleet.clients.

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!