实现数组转对象 [{{label: ‘男’, value: 0}}]转换为{0: ‘男’, 1:‘女’}格式
const arr = [{label: '男', value: 0},{label: '女', value: 1}]
function f(arr) {
let obj = {};
for (var item in arr){
obj[arr[item].value] = arr[item].label;
};
return obj
}
const obj = f(arr)
console.log(obj);
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));
const arr = [{ staff: 1 }, { staff: 2 }, { staff: 1 }, { staff: 1 }, { staff: 1 }, { staff: 2 }]
const hireType= [
{ id: 1, value: '正式' },
{ id: 2, value: '非正式' }
]
const obj = hireType.reduce((acc, cur) => {
acc[cur.id] = cur.value
return acc
}, {})
console.log(obj);
console.log(obj[arr[0]['staff']])
console.log(obj[arr[1]['staff']])