JS string to array, go to object method

Source: Internet
Author: User
Tags array to string

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.