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

140
Visualizações
how to convert an array of strings to array of numbers?

I have this part of project that I need to convert a Number to an Array, then I have to add +1 to every digit I wrote this code, still it return undefined

function Nbr1(a){
    return a.toString().split('').map(Number).forEach( (item, index) => item + 1);
}
console.log(Nbr1(12345))

and I got this result

enter image description here

and when I add console.log to every item in forEach, I got this

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You don't need to use a forEach after map. map already iterates through the array.

And don't forget to convert the array item to a number with a + before it because you generated an array of strings

function Nbr1(a) {
  return a.toString().split("").map((number) => +number + 1)
}

console.log(Nbr1(12345))

Learn more about map

about 4 years ago · Juan Pablo Isaza Relatório

0

See short example below with usage of parseInt(). No extra forEach required.

function Nbr1(a){
    return a.toString().split('').map((number)=>{
        return parseInt(number) + 1
    })    
}
console.log(Nbr1(12345))

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