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

117
Views
js object nested array filter based on array
const cars = [
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: (3) ['2022/05/28', '2022/06/02', '2022/06/05']
    },
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: (3) ['2022/05/30', '2022/06/02', '2022/06/05']
    },
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: (3) ['2022/05/28', '2022/06/01', '2022/06/05']
    }
]

I want to filter my object array based on another array

const arr2 = ["2022/06/02", "2022/06/05"];

I want to get results like that;

[
   {
     brand: "Chevrolet",
     model: "Camaro",
     dates: (3) ['2022/05/28', '2022/06/02', '2022/06/05']
   },
   {
     brand: "Chevrolet",
     model: "Camaro",
     dates: (3) ['2022/05/30', '2022/06/02', '2022/06/05']
   }
]

I used includes

let filtered = cars.filter((item) => item.dates.includes(arr2))

I got empty array.

when I pass a string to in includes I got filtered array.

let filtered = cars.filter((item) => item.dates.includes("2022/06/02"))

No I need to compare arr2. Its' length could be changed.

How can I pass an array to cars.include() function

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

[{}, {}].includes({})

returns false because it compares objects by object reference, not values in the object, so it's equivalent to

const a = {}; // new, empty object
const b = {}; // a different new, empty object
const c = {}; // yet another new, empty object
const answer = [a, b].includes(c) // false

Also, arrays are objects!

So that's why, when you pass an object (whether or not it's an array) into Array.prototype.includes, it returns false unless that same object is an element of the array.

You probably want something like this:

// Function that returns true if two arrays intersect
const includesAny = (arr1, arr2) => arr1.some(e1 => arr2.includes(e1));

const filterCarsByDates = (
  datesSought => cars.filter(
    car => includesAny(
      cars.dates,
      datesSought
    )
  )
);
about 4 years ago · Juan Pablo Isaza Report

0

let cars = [
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: ['2022/05/28', '2022/06/02', '2022/06/05']
    },
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: ['2022/05/30', '2022/06/02', '2022/06/05']
    },
    {
        brand: "Chevrolet",
        model: "Camaro",
        dates: ['2022/05/28', '2022/06/01', '2022/06/05']
    }
];
    
let arr2 = ["2022/06/02", "2022/06/05"];

// A method that checks if `src` array contains all elements of `target` array
let checker = (src, target) => target.every(v => src.includes(v));  

let filtered = cars.filter((item) => checker(item.dates, arr2));  

console.log(filtered);

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array Filter with Set, Searching in Set is faster than Array.

const cars = [
  {
    brand: "Chevrolet",
    model: "Camaro",
    dates: ["2022/05/28", "2022/06/02", "2022/06/05"],
  },
  {
    brand: "Chevrolet",
    model: "Camaro",
    dates: ["2022/05/30", "2022/06/02", "2022/06/05"],
  },
  {
    brand: "Chevrolet",
    model: "Camaro",
    dates: ["2022/05/28", "2022/06/01", "2022/06/05"],
  },
];

const arr2 = ["2022/06/02", "2022/06/05"];

const result = cars.filter((car) => {
  const set = new Set(car.dates);
  return arr2.every((date) => set.has(date));
});

console.log(result);

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!