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

194
Vistas
Finding Duplicates in array of object in javascipt by map method

I am trying to remove duplicate from an array of object in Javascript. Below is the code which doesnot work, it is creating Array of map of size 4.

let arr = [{id: 32, name: 'Name1', age: 88},
  {id: 33, name: 'Name2', age: 88},
  {id: 32, name: 'Name3', age: 88},
  {id: 1, name: 'Sam', age: 88},
  {id: 33, name: 'Name2', age: 88},
  {id: 2, name: 'Tom', age: 88}
];

console.log(arr)

let mp1 = new Map();
let uniqueElements = arr.map( obj => mp1.set(obj.id, obj))

console.log(uniqueElements);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Below snippet may be one possible implementation to achieve the desired objective:

const discardDupeIdElements = arr => (
  Object.values(
    arr.reduce(
      (fin, itm) => ({
        ...fin,
        [itm.id]: itm
      }), {}
    )
  )
);

var origArr = [{
    id: 32,
    name: 'Name1',
    age: 88
  },
  {
    id: 33,
    name: 'Name2',
    age: 88
  },
  {
    id: 32,
    name: 'Name3',
    age: 88
  },
  {
    id: 1,
    name: 'Sam',
    age: 88
  },
  {
    id: 33,
    name: 'Name2',
    age: 88
  },
  {
    id: 2,
    name: 'Tom',
    age: 88
  }
];

console.log(discardDupeIdElements(origArr));

Note

This will always keep the last element of a set of duplicates and ignore all earlier ones. Such as for id 32, it will keep the element with name = Name3 and discard Name2.

Fixing the OP's code:

const arr = [{id: 32, name: 'Name1', age: 88},
  {id: 33, name: 'Name2', age: 88},
  {id: 32, name: 'Name3', age: 88},
  {id: 1, name: 'Sam', age: 88},
  {id: 33, name: 'Name2', age: 88},
  {id: 2, name: 'Tom', age: 88}
];

// console.log(arr);
// commented-out for easier viewing of the output

const mp1 = new Map();      // declared a map

arr.forEach(obj => mp1.set(obj.id, obj));
// used .forEach to iterate through the array
// and update the map

const uniqueElements = [...mp1.values()];
// used spread operator to extract 
// the values of the map into an array

console.log(uniqueElements);
// console.log the unique elements

Note

Where possible, used const rather than let.

about 4 years ago · Juan Pablo Isaza Denunciar

0

map returns the new array with the same length of the calling array

For accumulated value (here is mp1), you should use reduce

let uniqueElements = arr.reduce(
  (mp1, obj) => mp1.set(obj.id, obj),
  new Map()
)

let arr = [
  { id: 32, name: 'Name1', age: 88 },
  { id: 33, name: 'Name2', age: 88 },
  { id: 32, name: 'Name3', age: 88 },
  { id: 1, name: 'Sam', age: 88 },
  { id: 33, name: 'Name2', age: 88 },
  { id: 2, name: 'Tom', age: 88 },
]

console.log(arr)
let uniqueElements = arr.reduce((mp1, obj) => mp1.set(obj.id, obj), new Map())
console.log(uniqueElements)

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