数组转对象
要求 let arr = [{ label: ‘男’, value: 0 }, { label: ‘女’, value: 1 }] 转换为 let obj ={0:‘男’,1:‘女’}
const arr = [{ label: '男', value: 0 }, { label: '女', value: 1 }]
function arrToObj(arr){
const obj = {}
arr.forEach((item)=>{
obj[item.value] = item.label
})
return obj
}
console.log(arrToObj(arr))
对象转数组
要求 let obj = { 0: ‘男’, 1: ‘女’ } 转为 let arr = [{ label: ‘男’, value: 0 }, { label: ‘女’, value: 1 }]
const obj = {
0:'男',
1:'女'
}
function objToArr(obj){
const arr = []
for(const key in obj){
arr.push({
lebel:obj[key]
value:key
})
}
return arr
}
console.log(objToArr(obj))