对象内key值替换

267 阅读1分钟
一个数组中有多个对象,将每个对象的中文key值替换成英文

11.png

//传入数组
handleSuccess(results){
  //中文key对应的英文key
  const userRelations = {
    '入职日期': 'timeOfEntry',
    '手机号': 'mobile',
    '姓名': 'username',
    '转正日期': 'correctionTime',
    '工号': 'workNumber',
  }
  const list = []
  results.forEach(item => {
    const obj = {}
    for (const key in item) {
      const englishKey = userRelations[key]
      obj[englishKey] = item[key]
    }
    list.push(obj)
  })
  console.log(list);
}