对象与数组相互转换

对象转数组

let arr = []
 
let obj = {
    dr: "dr001",
    ljy: "ljy002"
}
 
for(let prop in obj) {
    arr.push({
        name: prop,
        ct: obj[prop]
    })
}

数组转对象

let arr = [{
    name: 'dr',
    ct: 'dr001'
},
{
    name: 'ljy',
    ct: 'ljy002'
}]
let obj = {}
arr.forEach(item=> {
    obj[item.name] = item.ct
})
 
console.log('obj', JSON.stringify(obj, null, 2));

你可能感兴趣的:(对象与数组相互转换)