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

168
Views
Javasript, elimine TODOS, REALMENTE TODOS los duplicados del objeto

Tengo un objeto con notas, entradas de tiempo, quiero eliminar TODAS (TODAS, perdón por repetir esta parte, he revisado muchos ejemplos y todos eliminan solo el segundo, tercer, etc. duplicado, pero incluyen el primer duplicado) notas con DUPLICADOS al mismo tiempo de eso.

Bueno, necesito separar los únicos de los duplicados, logré obtener todos los duplicados con este código que encontré aquí en StackOverflow,

 const allNotes = [ { "note": 69, "time": 0 }, { "note": 57, "time": 0 }, { "note": 60, "time": 1.5 }, { "note": 64, "time": 2 }, { "note": 69, "time": 2.5 }, { "note": 71, "time": 3 }, { "note": 52, "time": 3 }, { "note": 64, "time": 4.5 }, { "note": 68, "time": 5 }, { "note": 71, "time": 5.5 } ] const getDuplicates = () => { const values = allNotes; const unique = new Object; const lookup = values.reduce((a, e) => { a[e.time] = ++a[e.time] || 0; return a; }, {}); const duplicates = values.filter(e => lookup[e.time]); console.log(duplicates); }

Y este código funciona como el encanto que produce.

 [ { "note": 69, "time": 0 }, { "note": 57, "time": 0 }, { "note": 71, "time": 3 }, { "note": 52, "time": 3 } ]

Todavía necesito poder obtener solo entradas de tiempo únicas, necesito este resultado

 [ { "note": 60, "time": 1.5 }, { "note": 64, "time": 2 }, { "note": 69, "time": 2.5 }, { "note": 64, "time": 4.5 }, { "note": 68, "time": 5 }, { "note": 71, "time": 5.5 } ]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Supongo que estás buscando algo como esto:

 array.filter(a => array.filter(b => a.time === b.time).length === 1) // filter array and sort for duplicates, just keep element if time-property is unique

Vea un ejemplo de trabajo a continuación:

 const allNotes=[{note:69,time:0},{note:57,time:0},{note:60,time:1.5},{note:64,time:2},{note:69,time:2.5},{note:71,time:3},{note:52,time:3},{note:64,time:4.5},{note:68,time:5},{note:71,time:5.5}]; const res = allNotes.filter(a => allNotes.filter(b => a.time === b.time).length === 1); console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Ya resolvió su problema ya que puede seleccionar los duplicados. Solo filtra lo contrario.

 const uniqueValues = values.filter(e => !lookup[e.time]);
about 4 years ago · Juan Pablo Isaza Report

0

const _dedupArr: any[] = []; allNotes.forEach((v, i) => { const find = _dedupArr.find(d => d.time == v.time); if(!find) { _dedupArr.push(v); } }); console.log('_dedupArr', _dedupArr);
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!