I have this string
'john smith~123 Street~Apt 4~New York~NY~12345'
Using JavaScript, what is the fastest way to parse this into
var name = "john smith";
var street= "123 Street";
//etc...
I have this string
'john smith~123 Street~Apt 4~New York~NY~12345'
Using JavaScript, what is the fastest way to parse this into
var name = "john smith";
var street= "123 Street";
//etc...
well, easiest way would be something like:
If Spliter is found then only
Split it
else return the same string
You don't need jQuery.
Something like:
Is probably going to be easiest
According to ECMAScript6
ES6
, the clean way is destructing arrays:You may have extra items in the input string. In this case, you can use rest operator to get an array for the rest or just ignore them:
I supposed a read-only reference for values and used the
const
declaration.Enjoy ES6!
Zach had this one right.. using his method you could also make a seemingly "multi-dimensional" array.. I created a quick example at JSFiddle http://jsfiddle.net/LcnvJ/2/