15

Is hasOwnProperty() method case-sensitive? Is there any other alternative case-insensitive version of hasOwnProperty?

JSuar
  • 21,056
  • 4
  • 39
  • 83
Arjun
  • 6,501
  • 10
  • 32
  • 34
  • Java Script is not case insensitive, so there's no alternative on that. But why you want it case insensitive? – Ortiga Apr 29 '11 at 13:52
  • Related: [Are javascript object keys case-sensitive?](https://stackoverflow.com/q/42400548/1048572), [Access JavaScript property case-insensitively?](https://stackoverflow.com/q/12484386/1048572) – Bergi Dec 14 '20 at 21:28

3 Answers3

19

Yes, it's case sensitive (so obj.hasOwnProperty('x') !== obj.hasOwnProperty('X')) You could extend the Object prototype (some people call that monkey patching):

Object.prototype.hasOwnPropertyCI = function(prop) {
      return ( function(t) {
         var ret = [];
         for (var l in t){
             if (t.hasOwnProperty(l)){
                 ret.push(l.toLowerCase());
             }
         }
         return ret;
     } )(this)
     .indexOf(prop.toLowerCase()) > -1;
}

More functional:

Object.prototype.hasOwnPropertyCI = function(prop) {
   return Object.keys(this)
          .filter(function (v) {
             return v.toLowerCase() === prop.toLowerCase();
           }).length > 0;
};
KooiInc
  • 119,216
  • 31
  • 141
  • 177
3

Old question is old; but still entirely relevant. I searched for a quick answer to this question as well, and ended up figuring out a good solution before I read this, so I thought I'd share it.

Object.defineProperty(Object, 'hasOwnPropertyCI', {
    enumerable: false,
    value: (keyName) => (
        Object.keys(this).findIndex(
            v => v.toUpperCase() === keyName.toUpperCase()
        ) > -1
    }
});

This resolves to true when keyName exists in the object it's called on:

var MyObject = { "foo": "bar" };
MyObject.hasOwnPropertyCI("foo");

Hope that helps someone else! :D

PS: Personally, my implementation slaps the conditional above into an IF statement, since I won't be using it anywhere else in my application (in addition to the fact that I'm not a huge fan of manipulating native prototypes).

Swivel
  • 3,020
  • 26
  • 36
  • I like the implementation. The question I have now is how to refer the property if we don't know its case? Say, if (!this.states.hasOwnPropertyCI(area)) { this.states[area] = {}; } if (!this.states[area].hasOwnPropertyCI(page)) { this.states[area][page] = {}; } How should I use this.states[area] to be able to refer to this property without having area in correct case? – Naomi Mar 21 '18 at 19:35
  • @Naomi Technically you could modify hasOwnPropertyCI to _return_ the key's actual name if it finds it, rather than just `true`. You can do so simply by using `.find()` instead of `.findIndex()`, and removing the `> -1` – Swivel Sep 19 '18 at 20:25
3

Yes, it's case sensitive, because JavaScript is case sensitive.

There is no alternative built-into the language, but you could roll your own:

function hasOwnPropertyCaseInsensitive(obj, property) {
    var props = [];
    for (var i in obj) if (obj.hasOwnProperty(i)) props.push(i);
    var prop;
    while (prop = props.pop()) if (prop.toLowerCase() === property.toLowerCase()) return true;
    return false;
}
Stoive
  • 11,232
  • 4
  • 24
  • 32
  • 2
    I won't ask *why* you want to do this, I'm sure there's a good reason... also, if you want the actual property in its correct case, replace `return true` with `return prop` – Stoive Apr 29 '11 at 13:58
  • 1
    I commonly use a case in-sensitive check when parsing data from external systems or that might be user entry, because javascript is sensitive, I need to make sure the input is correct. – Chris Schaller Oct 23 '15 at 04:30