Created
June 5, 2014 12:36
-
-
Save reacher-lu/17b04cf5ec10ef7df7b3 to your computer and use it in GitHub Desktop.
数组,字符串,对象相互转换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JavaScript 数组转字符串,字符串转数组 | |
一、数组转字符串(将数组元素用某个字符连接成字符串) | |
var a, b; | |
a = new Array(0,1,2,3,4); | |
b = a.join("-"); | |
二、字符串转数组(将字符串按某个字符切割成若干个字符串,并以数组形式返回) | |
var s = "abc,abcd,aaa"; | |
ss = s.split(",");// 在每个逗号(,)处进行分解。 | |
JavaScript 对象转字符串,字符串转对象 | |
1.eval | |
var str = '{ "name": "Violet", "occupation": "character" }'; | |
var obj = eval('(' + str + ')'); | |
2.JSON.parse | |
var str = '{ "name": "Violet", "occupation": "character" }'; | |
var obj = JSON.parse(str); | |
对象转字符串 | |
JSON.stringify(str); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment