Check for matching key in object regardless of capitalization

核能气质少年 提交于 2019-12-02 07:57:25

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
PPB

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!