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

275
Views
Eliminar objetos duplicados en JavaScript Array no funciona

En primer lugar, soy consciente de que hay MUCHAS respuestas en SO sobre esto, pero tengo algunos problemas con ellas, por eso publico otra pregunta sobre este tema.

Así que aquí está mi matriz de objetos:

 0: {id: 'lAYOUT', label: 'lAYOUT', items: 'val1'} 1: {id: 'tecst', label: 'tecst', items: 'val1'} 2: {id: 'tecst', label: 'tecst', items: 'val1'}

Estoy tratando de filtrar que solo habría 2 valores, ya que hay 2 objetos en la matriz que son iguales. Me gustaría hacer objetos únicos por items y label .

Así es como estoy tratando de hacerlo con lodash :

 const filteredArray = uniq(nestedItems, (item, key, a) => item.items && item.label)

Pero sigue devolviéndome los 3 elementos todavía.

También lo probé así:

 const filteredArray = [...new Set(nestedItems)]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Usando Filter obtenga el value, index and array . Usando FindIndex obtenga el objeto de matriz particular. y compare el filter object y el objeto de findindex object de búsqueda, si devuelve falso, ¡entonces presione una nueva matriz! ¡y crea una nueva matriz única!

¡Prueba este código!

 let arr = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }]; let newArr = arr.filter((value, index, self) => index === self.findIndex((t) => ( t.label === value.label && t.items === value.items )) ); console.log(newArr, 'newArr');
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar la agrupación hash para filtrar por varias claves:

 const data = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }]; const unique = Object.values(data.reduce((acc, obj) => { const hash = `${obj.id}-${obj.label}`; acc[hash] = obj; return acc; }, {})); console.log(unique);
 .as-console-wrapper{min-height: 100%!important; top: 0}

Mismo resultado con lodash

 const data = [{ id: 'lAYOUT', label: 'lAYOUT', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }, { id: 'tecst', label: 'tecst', items: 'val1' }]; const result = _.uniqWith(data, (o1, o2) => `${o1.id}-${o1.label}` === `${o2.id}-${o2.label}`); console.log(result);
 .as-console-wrapper{min-height: 100%!important; top: 0}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js" integrity="sha512-2iwCHjuj+PmdCyvb88rMOch0UcKQxVHi/gsAml1fN3eg82IDaO/cdzzeXX4iF2VzIIes7pODE1/G0ts3QBwslA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report

0

 const data = [ { id: 'id1', label: 'label1', items: 'items1' }, { id: 'id2', label: 'label2', items: 'items2' }, { id: 'id1', label: 'label1', items: 'items2' } ]; const unique = (...keys) => [ ...new Map(data.map(item => [keys.map(key => item[key]).join(), item])).values() ]; console.log(1, unique('label')); console.log(2, unique('label','items'));

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!