字符串变数组
let arr = 'gehe'
arr.split('') //[]
//将输入数组分开 [2,20,23,4,8] -> [2,[2,0],[2,3],4,8]
for(let i=0;i<a.length;i++) {
arr.push((a[i].toString().split('')).map(Number))
}
数组变字符串
let arr = [1,2,4,3,4]
//方法一
arr.join() //1,2,4,3,4
arr.join('') //12434
//方法二
arr.toString() //1,2,4,3,4
var arr = [1,[2,3],[4,5], [6,[7,8]]];
arr.toString() //1,2,3,4,5,6,7,8
数组变对象
一、数组中分割成对象
b = ['key=2','key=3','test=4']
//直接添加方括号
b //['key=2','key=3','test=4'] //针对对象包含数组
b.forEach(item => {
let keys = item.split('=')[0]
let val = item.split('=')[1]
res[keys]? res[keys] = [].concat(res[keys],val) :res[keys] = val
})
res //{'key':['2','3'],'test':'4'}
res[keys] = [res[keys], val] //法1: 中括号[] ,放数组4
res[keys] = [].concat(res[keys],val) //法2: [],concat填加数组
二、纯数组变对象
Object.assign(...arr1)
,{...arr}
let arr = [1,2,4,3,4,'d']
{...arr} //{0: 1, 1: 2, 2: 4, 3: 3, 4: 4, 5: "d"}
let arr1=[
{user:"123"},
{Cause:"22"},
{EnterFactoryTime:"33"},
{OutFactoryTime:"44"},
{VehicleGrade:"55"},
{IncomingInspection:"66"},
{Admission:"77"}
];
let arr2 = Object.assign(...arr1)
//{user: "123", Cause: "22", EnterFactoryTime: "33", OutFactoryTime: "44", VehicleGrade: "55",....}
对象变数组
let obj = {
'a' : 1,
'b' :2
}
Object.keys(obj) //["a", "b"]
Object.values(obj) //[1, 2]
const map = new Map()
map.set('a', 1)
map.set('b', 2)
[...map.keys()]; //["a", "b"]
[...map.values()] //[1, 2]