관리 메뉴

개발이야기

[ Javascript ] String to Array : 문자열을 배열로 변환 본문

node js/Javascript

[ Javascript ] String to Array : 문자열을 배열로 변환

안성주지몬 2019. 10. 8. 00:00

'1,2' 혹은 '123 456' 으로 구성되어있는 문자열을 배열로 어떻게 변환할 수 있을까요.

split() 함수를 사용하면 됩니다.

 

ex)

const temp = '1,2'

const tempToArray = temp.split(',')

console.log(tempToArray)

=> [1,2]

 

 

참고자료 

[1] https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array?noredirect=1&lq=1

 

Convert string with commas to array

How can I convert a string to a JavaScript array? Look at the code: var string = "0,1"; var array = [string]; alert(array[0]); In this case alert shows 0,1. If it where an array, it woul...

stackoverflow.com

 

Comments