Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda