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

174
Vistas
Filtering objects that has unique combination of values

I've got an array of objects that look like this:

[{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}]

From the objects above, there's some duplicate account names, but they're unique due to the account type. I'm trying to figure out a way to filter it so that a combination of key values would make an object unique, rather than a single key. Something like filtering the Account Name && Account Type.

I've also noticed some solutions would create a new array and include only the keys to filter, but I'd like the solution to preserve all keys from an object rather than creating a new object with lesser keys.

Expected result would look like this:

[{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd}]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const array = [{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}]

const filteredArr = array.reduce((acc, current) => {
  const x = acc.find(item => (item['Account Name'] === current['Account Name'] ) && (item['Account Type'] === current['Account Type']));
  if (!x) {
    return acc.concat([current]);
  } else {
    return acc;
  }
}, []);

console.log(filteredArr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use a Map. Loop over the array and on each iteration create a key from the Account Name and Account Type, and set the whole object as its value. Finally use values() to return a deduped array.

const data=[{"Account Name":"John Hill","Account Type":"Services",Organization:"A","Account Address":"2 Westall Rd"},{"Account Name":"John Hill","Account Type":"Training",Organization:"A","Account Address":"2 Westall Rd"},{"Account Name":"Dave Butcher","Account Type":"Engieering",Organization:"A","Account Address":"Level 1/55 Sunshine Blvd"},{"Account Name":"Jake Wellington","Account Type":"Management",Organization:"A","Account Address":"11 Maroochy Rd"},{"Account Name":"Jake Wellington","Account Type":"Management",Organization:"A","Account Address":"11 Maroochy Rd"}];

const map = new Map();

for (const obj of data) {
  const key = `${obj['Account Name']}-${obj['Account Type']}`;
  map.set(key, obj);
}

console.log([...map.values()]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the hash group approach to filter by several properties:

const data = [{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"},{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}];

const uniqHashes = data.reduce((acc, item) => {
    const hash = [item['Account Name'], item['Account Type']].join('-');
    acc[hash] ??= item
    return acc;
}, {});

const uniqs = Object.values(uniqHashes);

console.log(uniqs);
.as-console-wrapper { max-height: 100% !important; top: 0 }

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