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

149
Views
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 answers
Answer question

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 Report

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 Report

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 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!