数组<==>字符串
1.数组转换成字符串 Array.prototype.join(‘分隔符’)
var arr=[1,'w','er']
console.log(arr.join()) // 1,w,er
console.log(arr.join('.')) //1.w.er
console.log(arr.join('')) //1wer
2.字符串转换成数组 String.prototype.split(‘按什么切割的切割符’)
var str='name=lx&age=21&sex=男'
console.log(str.split('&')) // ["name=lx", "age=12", "sex=男"]
console.log(str.split('=')) // ["name", "lxl&age", "12&sex", "男"]
对象<==>字符串
1.对象转成字符串 JSON.stringify()
var obj={a:1,b:12,}
JSON.stringify(obj) // "{"a":1,"b":12}"
2.字符串转换成对象
var str='{"a":1,"b":12}'
JSON.parse(str) // {a: 1, b: 12}
对象<==>数组
1.数组转换成对象
var list = {};
var arr = ["123","456","789"];
for (var key in arr) {
list[key] = arr[key];
}
console.log(list);//{0: "123",1: "456",2: "789"}
2.对象转换成数组
let list = Array.from(object)
let list = Array.prototype.slice.call(object)
let list = [...object]