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

76
Visualizações
replace numeric string to number inside an array

I want to convert an array from

arr = ["step","0","instruction","1"]

to

newArr = ["step",0,"instruction",1]

here is my sample code:

  newArr = arr.map((x) => {
      if (typeof x === "number") {
        return x;
      }
    });
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could check if the string is convertable to a finite number and map the number in this case.

const
    data = ["step", "0", "instruction", "1"],
    result = data.map(v => isFinite(v) ? +v : v);

console.log(result);

If you need all other numbers as well, you could convert to number and check the the string of is against the value.

const
    data = ["step", "0", "instruction", "1", "NaN", "Infinity"],
    result = data.map(v => v === (+v).toString() ? +v : v);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are looking for something like this:

// Defining initial values
const initialValues = ["step","0","instruction","1"]

// Mapping initial values to a new parsed array
const parsedValues = initialValues.map(value => {
  // We try to parse the value. If it isNaN (Not a number) we just return the value e.g. don't change it
  if(isNaN(parseInt(value))) return value;
  // Else the value can be parsed to a number, so we return the parsed version.
  return parseInt(value);
})
// Printing the parsed results
console.log(parsedValues);

about 4 years ago · Juan Pablo Isaza Relatório

0

try this:

const arr = ["step","0","instruction","1"]

const newArray = arr.map(value => { //Iterate the array searching for possible numbers.
/* Check with function isNaN if the value is a number, 
  if true just return the value converted to number, 
  otherwise just return the value without modification */
  if (!Number.isNaN(Number(value))) { 
      return Number(value)
  }

  return value
})
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