将list数组中每个对象的键名转换为英文

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 将list数组中的每个对象键名转换为英文
        const list = [
            {'姓名':"张三",'年龄':17,'性别':"男",'爱好':"写代码"},
            {'姓名':"李四",'年龄':18,'性别':"男",'爱好':"看书"},
            {'姓名':"王五",'年龄':15,'性别':"男",'爱好':"打游戏"},
            {'姓名':"小明",'年龄':20,'性别':"男",'爱好':"学习"},
            {'姓名':"小红",'年龄':24,'性别':"女",'爱好':"追剧"},
        ]

        // 首先先定义一个中英文对应的对象
        const title = {
            '姓名': "name",
            '年龄': 'age',
            '性别': 'sex',
            '爱好': 'hobby',
        }

        // 定义一个新数组
        const newList = []

        list.forEach(item=>{
            const obj = {}
            // 拿到当前item的所有的键
            Object.keys(item).forEach(key=>{
                // key 当前item的键,比如:姓名
                // item[key] 当前item的值,比如:张三

                // title[key] 当前键对应的英文名:name 
                obj[title[key]] = item[key]
            })
            
            newList.push(obj)
        })

        console.log(newList);
    </script>
</body>

</html>
评论区
头像