1. Array to string join
var aarr = [1,2,2,2,2,3] varnull; = Aarr.join ("-"); Console.log (SSTR)//1-2-2-2-2-3 sStr = aarr.join ("+"); Console.log (SSTR)//1+2+2+2+2+3
2. String to Array
var sStr = "Abc,abcd,aaa"; var NULL ; = Sstr.split (",") Console.log (Aarr)// ["abc", "ABCD", "AAA"] Aarr = Sstr.split ("") Console.log (Aarr)//["A", "B", "C", ",", "a", "B", "C", "D", ",", "a", "a" , "a"] Aarr = Sstr.split ("-") Console.log (Aarr)//["ABC,ABCD,AAA"] the // string to array is cut into several strings by the split parameter and returned as an array
3. Transfer JSON string objects (note is JSON string)
varOobj ={name:Beijing, Age:1800, Value:Capital } varSSTR =json.stringify (oobj); Console.log (SSTR)//{"name": "Beijing", "Age": 1800, "value": "Capital"}Console.log (typeofSSTR)//string varSjsonstr = ' {' name ': ' Beijing ', ' age ': 1800, ' value ': ' Capital '} 'varOjsonobj =json.parse (SJSONSTR) console.log (ojsonobj)//{name: "Beijing", age:1800, Value: "Capital"}Console.log (typeofOjsonobj)//Object
4, non-string through Json.parse to object
var sstring = ' 121212 ' var sjsonstring = json.parse (sstring) Console.log (sjsonstring) //121212 console.log (typeof//number var sstring = ' nanjign ' var//uncaught syntaxerror:unexpected token A in JSON at posit Ion 1
By running the code, it is known that JS converts a string to another type of data. Try not to error, every opportunity.
JS string to array, go to object method