2
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

Nikhil
  • 81
  • 7
nihat onal
  • 37
  • 5
  • The problem is that you're trying to compare an array [arr2] to a string. https://stackoverflow.com/questions/34901593/how-to-filter-an-array-from-all-elements-of-another-array – Chris May 26 '22 at 09:54
  • Since the filte (`arr2`) is an array: you want the `dates` to include BOTH, or that include AT LEAST ONE of them ? By your expected result, looks like BOTH, but just asking for assurance. – Ergis May 26 '22 at 10:36
  • by the way.. since your problem is mainly how to check if an array contains any value from another array, you should look here: https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript ... array.includes expects a single value as argument to look for in the array – Diego D May 26 '22 at 11:22

3 Answers3

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
    )
  )
);
JSmart523
  • 2,069
  • 1
  • 7
  • 17
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);
Ergis
  • 1,105
  • 1
  • 7
  • 18
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);
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37