7

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 ?

cweiske
  • 30,033
  • 14
  • 133
  • 194
Gaurav Lath
  • 123
  • 1
  • 2
  • 8
  • 3
    JavaScript 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
  • 1
    yes 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 Answers3

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
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
-2

JavaScript like most languages is indeed case sensitive.

Bharel
  • 23,672
  • 5
  • 40
  • 80