Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!