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 ?
Asked
Active
Viewed 1.5k times
7
-
3JavaScript doesn't have dictionaries, it has objects. Aside from that, strings (which is really what you are talking about) are always case-sensitive. – Scott Marcus Feb 22 '17 at 19:28
-
1yes are case sensitive, but not dictionaries, only objects, but in newer revisions exists Map – Álvaro Touzón Feb 22 '17 at 19:28
-
You can always reduce the input to lower case and just compare everything as lower case. – Jon Glazer Feb 22 '17 at 19:29
-
http://stackoverflow.com/questions/12484386/access-javascript-property-case-insensitively – Hobroker Feb 22 '17 at 19:29
-
Related: [Access JavaScript property case-insensitively?](https://stackoverflow.com/q/12484386/1048572), [Is the hasOwnProperty method in JavaScript case sensitive?](https://stackoverflow.com/q/5832888/1048572), [Can an Object have case-insensitive member access?](https://stackoverflow.com/q/57530678/1048572) – Bergi Dec 14 '20 at 21:27
3 Answers
4
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.
You could apply a String#toLowerCase
to the key and use a function as wrapper for the access.
Example with a wrapper object.
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);

Nina Scholz
- 376,160
- 25
- 347
- 392
-
I am aware of that method. However, I am using the keys for data visualization and wanted to avoid changing the case. – Gaurav Lath Feb 22 '17 at 19:34
-
in this case, you could use a wraper object with lower case letters for checking. – Nina Scholz Feb 22 '17 at 19:36
0
Are javascript object keys case sensitive?
Yes, open your favorite browser's console to find your answer. I used Chrome:
var obj = {'one': 1}
obj.one
1
obj.ONE
undefined
obj['one']
1
obj['ONE']
undefined

abriggs
- 744
- 7
- 16