I have an array of strings. I'd like to extract a number suffix from a string. Here is an example string array.
let arrayData = [ '99ASD', '01A', '0134-A', '78134:TSX' ]
How can I extract numbers from each string?
I have an array of strings. I'd like to extract a number suffix from a string. Here is an example string array.
let arrayData = [ '99ASD', '01A', '0134-A', '78134:TSX' ]
How can I extract numbers from each string?
You can try using Array.prototype.map()
:
The
map()
method creates a new array populated with the results of calling a provided function on every element in the calling array.
and parseInt()
:
If
parseInt
encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point.parseInt
truncates numbers to integer values. Leading and trailing spaces are allowed.
let arrayData = [ '99ASD', '01A', '0134-A', '78134:TSX' ];
let arrayNum = arrayData.map(s => parseInt(s, 10));
console.log(arrayNum);