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

93
Vistas
sort unique values on the same line

If I have a list of items like this in any text-area, I can use a bookmarklet to sort the unique values.

a
b
d
c
c

I click on the bookmarklet and the list is corrected to:

a
b
c
d

code:

javascript: (
  function() {
    Array.from(document.querySelectorAll('textarea')).map(function(b) {
      var a = document.createElement('div');
      var d = document.createElement('button');
      d.textContent = '↑';
      d.addEventListener('click', function(f) {
        f.preventDefault();
        b.value = Array.from(new Set(b.value.split('\n'))).sort().join('\n')
      });
      var c = document.createElement('button');
      c.textContent = '↓';
      c.addEventListener('click', function(f) {
        f.preventDefault();
        b.value = Array.from(new Set(b.value.split('\n'))).sort().reverse().join('\n')
      });
      a.appendChild(d);
      a.appendChild(c);
      b.parentNode.insertBefore(a, b)
    })
  }
)();

But this does not work if all the items are on the same line like this...

a , b,d , c,c

Is it possible to modify the code to return the items like this...

a, b, c, d

I can do that in python. But I will like to know if it is possible using JavaScript bookmarklet.

myl = [i.strip() for i in text.split(',')]
myl.sort()
', '.join(set(myl))
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This would work better because since your input has blank spaces in it ,we have to use regex to split the string.

b = Array.from(
new Set(b.split(/[ ,]+/))).sort().reverse().join(',')
about 4 years ago · Juan Pablo Isaza Denunciar

0

On this line:

b.value = Array.from(
  new Set(b.value.split('\n'))).sort().reverse().join('\n')

You just need to change '\n' to ,, like:

b.value = Array.from(
  new Set(b.value.split(','))).sort().reverse().join(',');

b.value.forEach(item => item.trim()); //to remove trailing whitespaces
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could try lodash for this purpose, As mentioned in other answers that you need to first change the words format from : a b d c c to : a,b,d,c,c

if changed

let x= // your values e.g a,b,d,c,c
//
let valuesArray=_.split(x,',')
uniqueFromArray=_.uniq(valuesArray)

if unchanged

let x= // your current input values 
//
let valuesArray=_.split(x,'\n')
uniqueFromArray=_.uniq(valuesArray)
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