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

165
Vistas
Use String functions on an element while using map() on array in javascript?

I am trying to do the following.

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.sort(), s]);

Essentially, what I am trying to do is create a new array of arrays, where this new array is like [sorted string from first array, original string from first array]

However, it looks like the .sort() method is not valid here.

I even tried making it

let sorted_str = strs.map((s) => [s.toString().sort(), s]);

to force a String and make sure it has a sort() method possible, but to no avail.

The error is TypeError: s.toString(...).sort is not a function.

Any way I can get this to work or any simple workaround will be appreciated.

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

0

You need to get an array of characters, sort it and get a string back.

const
    strs = ["one", "two"],
    sorted_str = strs.map((s) => [Array.from(s).sort().join(''), s]);

console.log(sorted_str);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to convert to array, then apply the sort method, and then join to a string again, try this:

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.split('').sort().join(''), s])

console.log(sorted_str);

about 4 years ago · Juan Pablo Isaza Denunciar

0

instead of using split(). do not use split()

strs = ["one", "two"];
let sorted_str = strs.map((s) => [[...s].sort().join(''), s]);

console.log(sorted_str)

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