Try this:
var myObject = { "mIxeDCaSEKeY": "value" };
var searchKey = 'mixedCaseKey';
myObject[Object.keys(myObject).find(key => key.toLowerCase() === searchKey.toLowerCase())];
You can alternatively already provide the searchKey in lowercase.
If you want it as a function:
/**
* @param {Object} object
* @param {string} key
* @return {any} value
*/
function getParameterCaseInsensitive(object, key) {
return object[Object.keys(object)
.find(k => k.toLowerCase() === key.toLowerCase())
];
}
If the object can't be found, then it'll return undefined, just like normal.
If you need to support older browsers, then you can use filter
instead:
function getParameterCaseInsensitive(object, key) {
return object[Object.keys(object).filter(function(k) {
return k.toLowerCase() === key.toLowerCase();
})[0]];
}
I suggest using the polyfills for Object.keys() and Array.filter() if you need even older support.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…