Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda