js 数组、字符串、Json互相转换

arr.join(): 数组转字符串

let arr = [1,2,3,4];
let str = arr.join(',');

arr.split():字符串转数组

 

let str = '1,2,3,4';
let arr = str.split(',');

 

JSON.parse():从一个字符串解析成json对象

let data = '{"name":"Liudongdong"}'
JSON.parse(data);

 

JSON.stringify():把一个JSON对象中解析成字符串

let data = {"name":"Liudongdong"}
JSON.stringify(data);

 

Object.values():JSON单对象转换成数组

let json = {"name":"liudong","age":"18"};
Object.values(json);//["liudong","18"]

 

map对象:JSON数组对象转换成一维数组

let users = [
  {name: "one", "email": "one@email.com"},
  {name: "two",   "email": "two@email.com"},
  {name: "three",  "email": "three@email.com"}
];
let emails = users.map((user)=>{ return user.email; });

//["one@email.com","two@email.com","three@email.com"]

 

posted @ 2017-07-20 16:10  yudis  阅读(762)  评论(0编辑  收藏  举报