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

108
Views
Compare two arrays of objects and filter out similar property values?

I'm calling two API endpoints: the first one will return an array of clients whose account is currently active with us, and the second will return an array of ALL clients (current and past) regardless of their current status.

I'm creating a dropdown menu that will basically add a client to the "active client" array. To create this dropdown, I need to filter out all clients from the "All clients array" that already exist in the "Active clients array".

Eg:

ALL clients

[{name: "Martha", id: 1}, {name: "John", id: 2},{name: "Jane", id: 3}, {name: "Mary", id: 4}]

Active clients

[{name: "John", customerid: 2}, {name: "Mary", customerid: 4}]

(Yes, the backend dev did give them different property names)

My new array should obviously be:

[{name: "Martha", id: 1}, {name: "Jane", id: 3}]

I can iterate through this list to populate the dropdown menu and only submit a client to the backend that doesn't already have an active account with us.

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

0

If you can consider the ids to be unique then you could do something like this:

function removeDuplicates(allClients, activeClients) {
  const activeClientIds = new Set(
    activeClients.map((client) => client.customerid)
  );

  return allClients.filter((client) => !activeClientIds.has(client.id));
}

const allClientsArr = [
  { name: "Martha", id: 1 },
  { name: "John", id: 2 },
  { name: "Jane", id: 3 },
  { name: "Mary", id: 4 },
];
const activeClientsArr = [
  { name: "John", customerid: 2 },
  { name: "Mary", customerid: 4 },
];

const result = removeDuplicates(allClientsArr, activeClientsArr);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can use a simple .filter. Also, your example result is actually missing a element.

const all = [{name: "Martha", id: 1}, {name: "John", id: 2},{name: "Jane", id: 3}, {name: "Mary", id: 4}]
const old = [{name: "John", customerid: 2}, {name: "Mary", customerid: 4}];
const oldIds = old.map(customer => customer.customerid);
console.log(all.filter(customer => !(customer.id in oldIds)))

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!