Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda