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

153
Visualizações
How to filter two arrays?

How to filter two arrays?

I have two arrays where allBrands are all brands and userBrands are brands that the user has already selected, I'm trying to filter allBrands in such a way that it doesn't show the brands already selected by the user

  const allBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "bmw" },
    { id: 2, title: "mercedes" },
    { id: 3, title: "samsung" }
  ];
  const userBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "mercedes" }
  ];

  const filtered = allBrands.filter(({ title }) => {
    return userBrands.map((item) => item.title !== title);
  }); // need bmw, samsung
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

We can use find inside the filter instead of map to find if the brand is already selected by the user. If brand is found in userBrands then return false, else return true inside filter.

const allBrands = [
  { id: 0, title: "Apple" },
  { id: 1, title: "bmw" },
  { id: 2, title: "mercedes" },
  { id: 3, title: "samsung" }
];
const userBrands = [
  { id: 0, title: "Apple" },
  { id: 1, title: "mercedes" }
];

const filtered = allBrands.filter(({ title }) => {
  return !userBrands.find((item) => item.title === title);
}); // need bmw, samsung

console.log(filtered);

about 4 years ago · Santiago Trujillo Relatório

0

const allBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "bmw" },
    { id: 2, title: "mercedes" },
    { id: 3, title: "samsung" }
  ];
  const userBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "mercedes" }
  ];

  
  
  const filtered = allBrands.filter(({ title:title1 }) => !userBrands.some(({ title:title2 }) => title1 === title2));
  
  console.log(filtered)

about 4 years ago · Santiago Trujillo Relatório

0

If you are thinking of filtering the allBrands Array so that it does not contain the brands already selected by the user. You can try making 2 for loops and nesting one in the other and cycling through the allBrands array to check if it contains the brand already selected by the user. Example:

const allBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "bmw" },
    { id: 2, title: "mercedes" },
    { id: 3, title: "samsung" }
  ];
  const userBrands = [
    { id: 0, title: "Apple" },
    { id: 1, title: "mercedes" }
  ];
 
 //Filtering Part
 for(let i = 0; i < allBrands.length; i++) {
  for(let j = 0; j < userBrands.length; j++) {
    if(allBrands[i] === userBrands[j]) {
        allBrands.slice(i, 1);
    }
  }
}

about 4 years ago · Santiago Trujillo 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