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

268
Visualizações
Is it possible to change the entire array element in JavaScript to number type?

Ex)

const a = ['1' , '2' , '3']

When there is an array called , is there a way to change it all at once without looping?

i want result [1,2,3]

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

0

Kind of, but not really.

The best approach is likely to do an array map as such:

const a = ['1' , '2' , '3'];
const b = a.map(function(i) {return Number(i);});

The advantage to mapping is that it theoretically can be done in parallel instead of sequentially. However, JavaScript's particular implementation doesn't really allow for this because JS uses a single thread and a scheduler instead of a truly multi-threaded approach.

However, using mapping is a cleaner and better approach because if some day JavaScript implements multithreading, the map() method could theoretically be changed to go multithreaded. If that was ever done, your code would already benefit from that (very unlikely) change.

about 4 years ago · Juan Pablo Isaza Relatório

0

There is a way possible.

Read more about Array.map() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

It's very simple. You just have to do this:-

a.map(strN => (!isNaN(strN)) ? Number(strN) : strN)

*Notice: This will return the same value if the string isn't a number.

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