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

198
Views
TypeScript: How to replace a particular number in a string (a combination of numerical and non-numerical values), and then update its value?

I asked a similar question here TypeScript: How to replace a particular number in a string, and then update its value?

Really appreciate the answers there. It answered my questions.

I am run into a new issue. Thought it s more helpful to raise a new question while referring to the old question.

I have a column that is full of 10-digits string.

Sometimes that entire 10-digits only contain numerical values (e.g. 3345678901), but sometimes there is dash included (e.g. 3345678---)

I would like to be able to:

  1. Input an index number X
  2. Locate the corresponding number in the string
  3. Add or subtract a particular number A to/from that number to update the string
  4. Update the string

Example 1 (all numerical): 3345678901

  • Input index number "4"
  • The corresponding number is 6
  • Increase that number by +2 to 8
  • Update the string to 3345878901

Example 2 (numerical & non-numerical): 3345678---

  • Input index number "4"
  • The corresponding number is 6
  • Increase that number by +2 to 8
  • Update the string to 3345878---

Example 3 (numerical & non-numerical): 3345678---

  • Input index number "7"
  • The corresponding value is -
  • Increase (or rather, update) that number by +2 to 2
  • Update the string to 33458782--

For example 1, I know I could do following (as a contributor from the OG post has pointed out):

const givenStr = "3345678901";

let givenStrArray = givenStr.split('').map(Number);

const inputIndex = 4;
const increaseAmount = 2;

givenStrNumber += increaseAmount

console.log(givenStrNumber);

But, how do I go about Example 2 and 3 though? Since there are string '-' involved? In this case map(Number) would lead to Null values that would break the code.

Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Check for NaN. This is a tiny snippet that would give you a hint:

// ...
// ...
const increment = 2;

let givenStrArray = 
  givenStr
   .split('')
   .map((digiit) => isNaN(digit) ? increment : Number(digit + increment);

// ...
// ...
about 4 years ago · Juan Pablo Isaza Report

0

This should do the trick

let str = '3345678---'

let NumericStrArray = str.split('').map(x => Number.isNaN(Number(x)) ? x: Number(x))

let ipIndex = 7
let incAmount = 2
let val = NumericStrArray[ipIndex]
NumericStrArray[ipIndex] = typeof val === 'string' ? incAmount : val + incAmount

let result = NumericStrArray.join('')
about 4 years ago · Juan Pablo Isaza Report

0

Here's a simple little snippet. Break the sting apart, map through each item, when we find the index we need to update we update it and return it, then join them all back together.

Multiple conditional ternary operator have been used here, you can find out more about how they work here: Conditional (ternary) operator

const givenStr = "334567----", 
  inputIndex = 6,
  increaseAmount = 2

let givenNumber = givenStr
  .split('')
  .map((v,i) => i === inputIndex ? isNaN(v) ? increaseAmount : +v + increaseAmount : v)
  .join('')

console.log(givenNumber);

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!