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

262
Views
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 answers
Answer question

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 Report

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