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

205
Vistas
JavaScript - How to get the line after the second comma

I have a certain line I want to get from this line the word "USA" which is after the second comma, how can I get it? I have a lot of such lines, I need to get a certain word that is after the second comma

let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Simply splitting the string on commas and accessing the element after the 2nd one should be enough. As long as the line does indeed contain enough commas you can do:

const x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
const result = x.split(",")[2].trim();

console.log(result);

Please note i called String.prototype.trim() function simply to remove whitespace from the beginning of a string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can split the string and get the second index from the value. Use optional chaining .? to avoid the error.

let x = `Addr: Miami Beach, FL 321, USA, Distance: 7642 mi`;
let value = x.split(',')?.[2]?.trim() || ""
console.log(value)

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