1

Given a key: 'mykey'

And given an object: Object {Mykey: "some value", ...}

And using the following if (key in myObject) syntax to check for a match...

How can I check matching strings regardless of capital letters?

For example: key mykey should be matched to Mykey in the object even though the M is capitalized.


I am aware of a function to do this: How to uppercase Javascript object keys?

I was looking to see if there was another way.

Community
  • 1
  • 1
user3871
  • 12,432
  • 33
  • 128
  • 268
  • You can't, are you asking how to create a function that does that ? – adeneo May 28 '15 at 06:01
  • Your retrieval function would be accepting potential collisions in the map based on an arbitrary logic shim. I don't think this is a built-in feature, you're best bet might be to insert this as a custom function (like the example @adeneo gave) in the prototype chain and inherit from that. – Cristian Cavalli May 28 '15 at 06:11

2 Answers2

3

You can create a function that does this, there's no native case-insensitive way to check if a key is in an object

function isKey(key, obj) {
    var keys = Object.keys(obj).map(function(x) {
        return x.toLowerCase();
    });

    return keys.indexOf( key.toLowerCase() ) !== -1;
}

used like

var obj    = {Mykey: "some value"}
var exists = isKey('mykey', obj); // true
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

follow this example

var myKey = 'oNE';
var text = { 'one' : 1, 'two' : 2, 'three' : 3};
for (var key in text){
if(key.toLowerCase()==myKey.toLowerCase()){
//matched keys
    console.log(key)
}else{
//unmatched keys
    console.log(key)
}

}

JavaScript: case-insensitive search

Community
  • 1
  • 1
PPB
  • 2,937
  • 3
  • 17
  • 12