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

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

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

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

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 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