Checking the compliance of the key in the object regardless of its capitalization
Specified key: 'mykey'
And the object is given: Object {Mykey: "some value", ...}
And using the following syntax if (key in myObject)
to check the match ...
How can I check matching strings regardless of uppercase letters?
For example: the key mykey
must be mapped to mykey
in the object, even if a capital letter M
.
I am aware of a function for this: How are Javascript objects capitalized keys?
I was looking if there is another way.
You can create a function that does this, there is 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 as
var obj = {Mykey: "some value"}
var exists = isKey('mykey', obj); // true
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