对比两个文件属性名是否都一样(JavaScript)

80 阅读1分钟

对比两个文件中对象属性名是否相同

//  en.js
const en = {
  accountSecurity_modify_password: "Modify Password",
  accountSecurity_modify: "Modify",
  accountSecurity_middle: "Middle",
  accountSecurity_security_level: "Security level",
  accountSecurity_security_level_desc:
    'Your security level is "medium." To protect the security of your account, please enable Authenticator verification',
  accountSecurity_password_recommended:
    "A high-security password can make the account more secure. It is recommended that you regularly change the password, and set a password that contains numbers and letters and is longer than 6 digits.",
  accountSecurity_payment_password: "Payment password",
  accountSecurity_payment_password_desc:
    "The password required to be entered when using the Hagobuy balance for payment, and the payment password is set to ensure payment security.",
  accountSecurity_recently_landed: "Recently landed",
};
export default en;
// zh.js
const zh ={
    accountSecurity_modify_password: "修改密码",
    accountSecurity_modify: "立即修改",
    accountSecurity_middle: "中",
    accountSecurity_security_level: "安全级别",
    accountSecurity_security_level_desc:
      "你的安全级别是“中等”,为保护您的账户安全,请开启Authenticator验证",
    accountSecurity_password_recommended:
      "安全性高的密码可以使账号更安全。建议您定期更换密码,且设置一个包含数字和字母,并长度超过6位以上的密码。",
    accountSecurity_payment_password: "支付密码",
    accountSecurity_payment_password_desc:
      "使用Hagobuy余额支付时需要输入的密码,设置支付密码以保障支付安全。",
    accountSecurity_recently_landed: "最近登陆",
    accountSecurity_view_location_and_ip: "查看账户的登陆时间.位置和IP",
    accountSecurity_check_records: "查看记录",
    accountSecurity_change_email: "修改邮箱",
}

export default zh;
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Execute Code</title>
</head>
<body>

<button id="executeButton">开始执行</button>

<script>
document.getElementById("executeButton").addEventListener("click", function() {
    import('./en/en.js').then(({default: en}) => {
        import('./en/zh.js').then(({default: zh}) => {
            const enKeys = Object.keys(en);
            const zhKeys = Object.keys(zh);
            const differentKeys = enKeys.filter(key => !zhKeys.includes(key)).concat(zhKeys.filter(key => !enKeys.includes(key)));
            const enDifferent = Object.fromEntries(Object.entries(en).filter(([key]) => differentKeys.includes(key)));
            const zhDifferent = Object.fromEntries(Object.entries(zh).filter(([key]) => differentKeys.includes(key)));

            console.log("enDifferent:", enDifferent); // en.js 对比结果不同项
            console.log("zhDifferent:", zhDifferent); // zh.js 对比结果不同项
        });
    });
});
</script>

</body>
</html>

// 输出结果
zhDifferent: {
    "accountSecurity_view_location_and_ip": "查看账户的登陆时间.位置和IP",
    "accountSecurity_check_records": "查看记录",
    "accountSecurity_change_email": "修改邮箱"
}