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

200
Vistas
Remove objects if they are duplicated with javascript

I'm trying to remove duplicated objects if the ids are the same with lodash _.uniqBy.

Or this doesn't have to be a lodash or _.uniqBy, but is this possible?

Also the id value is always dynamic, so this time I want to remove the object with id:123, but other time it will be different id

import _ from "lodash";

export default function App() {
  const samples = [
    { id: 123, name: "Dan", age: 10 },
    { id: 234, name: "Jake", age: 22 },
    { id: 123, name: "Jake", age: 9 }
  ];

  let uniq = _.uniqBy(samples, samples.name);

  console.log(uniq);
  return (
    <div className="App">
      {uniq.map((u) => (
        <p>{u.id}</p>
      ))}
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Or you can take a look at sample code here

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

0

You just need to provide the property name which is name in your case. See _.uniqBy() docs.

const samples = [{
    id: 123,
    name: "Dan",
    age: 10
  },
  {
    id: 234,
    name: "Jake",
    age: 22
  },
  {
    id: 123,
    name: "Jake",
    age: 9
  }
];

const unique = _.uniqBy(samples, 'name');
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Edit

As the question was edited: If you want to have it unique for id you should pass "id" instead of "name".

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