中文键转换成英文键

136 阅读1分钟

1. 先在函数内定义好需要处理的数据

const arr = { 一: 'one', 二: 'two', 三: 'three' }

2. 定义一个空对象

const newArr = {}

3. 调用Object.keys()方法获取中文键数组

const chKeys = Object.keys(arr)

4. 循环遍历获取中文键对应的英文键并且将其赋值

chKeys.forEach(key => { const enKeys = arr[key] newArr[enKeys] = key })

5. 最终代码和结果

fn() { 三: 'three' } console.log(chKeys) chKeys.forEach(key => { const enKeys = arr[key] newArr[enKeys] = key }) console.log(newArr) }

结果:
 

image.png