javascript对象键区分大小写吗? [英] Are javascript object keys case sensitive?

查看:43
本文介绍了javascript对象键区分大小写吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过对象键来修复javascript数组中的重复项.该循环在同一对象中将虚拟现实"和虚拟现实"添加为不同的键.有没有办法使Javascript对象不区分大小写?

I was trying to fix duplicate items in an array on javascript by the means of object keys. The loop added 'virtual reality' and 'Virtual Reality' in the same object as different keys. Is there a way to make Javascript object not case sensitive ?

推荐答案

While object properties are strings and they are case sensitive, you could use an own standard and use only lower case letters for the access.

您可以应用 将String#toLowerCase 插入键,并使用函数作为访问的包装器.

You could apply a String#toLowerCase to the key and use a function as wrapper for the access.

带有包装对象的示例.

function insert(key, value) {
    if (!wrapper[key.toLowerCase()]) {
        wrapper[key.toLowerCase()] = key;
    }
    data[wrapper[key.toLowerCase()]] = value;
}

var data = {},
    wrapper = {};

insert('Foo', 'bar');
console.log(data);
insert('FOO', '42');
console.log(data);

这篇关于javascript对象键区分大小写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆