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

133
Views
no puede devolver elementos únicos de coincidencias

Quiero devolver _1, _2 y no _1, _2, _1:

 let regex = /_[0-9]/g; let string = 'a_1 b_2 c_1'; let matches = [...string.matchAll(regex)]; let unique = [...new Set(matches)]; alert(unique);

¿No ves por qué no elimina los duplicados?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Está utilizando .matchAll , que devolverá un iterador que, cuando se convierte en una matriz, da como resultado:

 [ ['_1', index: 1, input: 'a_1 b_2 c_1', groups: undefined], ['_2', index: 5, input: 'a_1 b_2 c_1', groups: undefined], ['_1', index: 9, input: 'a_1 b_2 c_1', groups: undefined] ]

Por lo tanto, la deduplicación de los elementos de la matriz con un Conjunto no funciona: las matrices nunca son === entre sí.

Use .match en lugar de .matchAll para obtener una matriz de coincidencias (cadenas), no una matriz de matrices.

 const regex = /_[0-9]/g; const string = 'a_1 b_2 c_1'; const matches = string.match(regex); const uniqueMatches = [...new Set(matches)]; console.log(uniqueMatches);

about 4 years ago · Juan Pablo Isaza Report

0

Sin usar un método diferente, matchAll podría no ser el mejor aquí, aplane la matriz de coincidencias antes de pasar al constructor Set:

 let regex = /_[0-9]/g; let string = 'a_1 b_2 c_1'; let matches = [...string.matchAll(regex)].flat(); let unique = [...new Set(matches)]; console.log(unique);

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!