使用reduce实现随想转数组,数组转对象
let obj = {
1:'aa',
2:'bb',
3:'cc',
4:'dd',
}
let obj_1 = [
{
label:'name',
value:'xioaming'
},
{
label:'age',
value:12
}
]
let result_1 = obj_1.reduce((aa,itm)=>{
aa[itm.label] = itm.value;
return aa;
},{})
let result = Object.keys(obj).reduce((rest,item)=>{
let a = {}
a['label'] = obj[item];
a['value'] = item;
rest.push(a);
return rest;
},[])
console.log(result_1,result);
//{ name: 'xioaming', age: 12 }
// [
// { label: 'aa', value: '1' },
// { label: 'bb', value: '2' },
// { label: 'cc', value: '3' },
// { label: 'dd', value: '4' }
// ]