无论大小写如何,都要检查对象中的匹配键 [英] Check for matching key in object regardless of capitalization
问题描述
给定一个键:'mykey'
给定一个对象: Object {Mykey:some value,...}
并使用以下 if(myObject中的键)
检查匹配的语法...
And using the following if (key in myObject)
syntax to check for a match...
如何检查匹配的字符串而不管大写字母?
How can I check matching strings regardless of capital letters?
例如:key mykey
应与对象中的 Mykey
匹配,即使 M
已大写。
For example: key mykey
should be matched to Mykey
in the object even though the M
is capitalized.
我知道要执行的功能这:如何大写Javascript对象键?
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.
推荐答案
你可以创建一个函数这样做,没有本地不区分大小写的方法来检查一个键是否在一个对象中
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;
}
使用类似
var obj = {Mykey: "some value"}
var exists = isKey('mykey', obj); // true
这篇关于无论大小写如何,都要检查对象中的匹配键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!