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