Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

javascript - split single string element of array in different integer elements of array in JS

I have a input in below format and contain only numbers not alphabets: arr = ['1 2 3 4 5 6']

I want to split all the numbers in below format:

arr = [1,2,3,4,5,6]

question from:https://stackoverflow.com/questions/65889030/split-single-string-element-of-array-in-different-integer-elements-of-array-in-j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can do it like so

arr[0].split(' ').map((n) => parseInt(n))

If array could contain multiple similar strings like so ['1 2 3 4', '5 6 7 8']

arr.map((string) => {
  string.split(' ').map((n) => parseInt(n))
}).flat();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...