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

283
Vistas
How to remove a part of a string which will always be at the last?

I made a function that removes the last part of the string which will always start from _ and get the integer, but all I get is the text part and not the integer

How can this be achieved?

function splitLast(arg) {
  if (arg.includes(".") && arg.includes("_")) {
    return parseFloat(arg.split("_").pop())
  } else if (arg.includes(".") != true && arg.includes("_")) {
    return parseInt(+arg.split("_").pop())
  } else {
    throw "Arguments passed does not contain the valid result characters or isnt a required Datatype for thefunction"
  }
}

console.log(splitLast("22_no"), 'should be 22')
console.log(splitLast("22.1_no"), 'should be 22.1')

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

0

UPD: A better solution

You can use parseFloat() function without additional checks and string parsing (it will do it all for you):

function splitLast(arg) {
  if (isNaN(arg.trim()[0])) {
    throw new Error('argument is not valid')
  }

  return parseFloat(arg)
}

console.log(splitLast("22_no"), 'should be 22')
console.log(splitLast("22.1_no"), 'should be 22.1')
console.log(splitLast("a22.1_no"), 'should throw')
console.log(splitLast("  a22.1_no"), 'should throw')

isNaN() check is needed because parseFloat(string) returns:

  1. A floating-point number parsed from the given string (which is exactly what you need)
  2. NaN when the first non-whitespace character cannot be converted to a number (which is an edge-case but we should know about it)

Old solution:

Probably, you have to use the shift() method instead of pop().


The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift


function splitLast(arg) {
  if (arg.includes(".") && arg.includes("_")) {
    return parseFloat(arg.split("_").shift())
  } else if (arg.includes(".") != true && arg.includes("_")) {
    return parseInt(arg.split("_").shift())
  } else {
    throw "Arguments passed does not contain the valid result characters or isnt a required Datatype for thefunction"
  }
}

console.log(splitLast("22_no"), 'should be 22')
console.log(splitLast("22.1_no"), 'should be 22.1')


about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use a regexp and .replace:

function splitLast(value) {
  return value.replace(/_.*$/, '');
}

console.log(splitLast("22_no"), 'should be 22')
console.log(splitLast("22.1_no"), 'should be 22.1')

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use this to remove text from string and only numbers with underscore remains.

 var ret = "22.1_no";
    var str = ret.replace(/[^0-9\.]+/g, "");
    console.log("_"+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