js设置键名和键值
let obj1 = {'jifeng': 60, 'lilu': 59, 'hongyan': 51, 'donghan': 4,
'liudan': 18, 'liuhaiyan': 30, 'sunlianghong': 20}
let obj2 = {'admin': '王雪', 'tangminhao': '唐敏昊', 'yuanshuai': '原帅', 'qiguihong': '戚桂洪', 'cairongrong': '蔡荣荣',
'shaohonggang': '邵洪岗', 'xiyaqiang': '郗亚强', 'xipengpeng': '奚朋朋', 'gaofei': '高飞', 'zhangshaopeng': '张少鹏',
'wuqigang': '吴其刚', 'wangtao': '王涛', 'yuwenhao': '余文豪', 'xuewei': '薛伟', 'caozhenjie': '曹贞杰',
'butingchao': '卜廷超', 'wanghongbo': '王宏波', 'minsheng': '闵生', 'shenying': '沈颖', 'zhengmingming': '郑明明',
'moweiwei': '莫伟伟', 'ronghui': '戎辉', 'zhujintao': '朱金韬', 'yandingdong': '颜定东', 'wulailian': '吴来莲',
'hujingang': '胡金刚', 'liqianheng': '李前衡', 'liudan': '刘丹', 'jifeng': '季峰', 'lilu': '李路', 'liuhaiyan': '柳海燕',
'chenlingxia': '陈玲霞', 'sunlianghong': '孙良红', 'hewei': '何伟伟', 'denglingling': '邓玲玲', 'qifie': '齐飞',
'xujingwen': '徐静文', 'hongyan': '洪燕', 'tangpei': '唐培', 'yeweici': '叶慰慈', 'zhanghui': '张辉', 'simeiling': '司美玲',
'shenyongfu': '沈永富', 'luhaijun': '陆海军', 'dongfang': '董方', 'zhaojiang': '赵江', 'zhubinbin': '朱彬彬',
'xuyuxin': '徐裕欣', 'mujiaming': '穆加明', 'wangdaitao': '王代涛', 'yangqiang': '杨强', 'pengzhicheng': '彭志成',
'jinfeiyu': '金飞宇', 'liyaorong': '李耀荣', 'xuguangtan': '许光坦', 'wangxue': '王雪', 'caoke': '曹珂',
'yaoqichang': '姚启昌', 'pangyanni': '庞燕妮', 'zhouliangyue': '周良玥', 'wangdongliang': '王栋樑', 'gengyujing': '耿俞静',
'donghan': '董涵', 'chenkai123': '陈凯', 'yunan': '俞楠', 'mahaichao': '马海超'}
// let res = Object.keys(obj1).map(value => ({obj2[value]:obj1[value]}))
let res = Object.keys(obj1).map(value => {
let newObj = {}
newObj[obj2[value]] = obj1[value]
return newObj
})
console.log(res)
是20行而不是19行,因为js中键名不能使表达式,只会定义不会执行,
newObj的键名是 obj2[value]的返回值。