如何查找JavaScript对象是否具有特定正则表达式模式的键?例如,在下面的对象中,如何查找它是否包含包含单词"Address"的键?
var obj = {Address_Line1 : "XXX", Address_Line2 :"YYY", Name : "ZZZ"};
Scott.. 5
当然 - 你可以这样做,Array.prototype.some
并且Object.keys
像这样:
var obj = {Address_Line1 : "XXX", Address_Line2 :"YYY", Name : "ZZZ"};
var hasKeyRegex = Object.keys(obj).some(function(key) {
return /Address/.test(key);
});
console.log(hasKeyRegex);
hasKeyRegex
将是true
如果对象具有包含密钥Address
,和false
如果不是.
当然 - 你可以这样做,Array.prototype.some
并且Object.keys
像这样:
var obj = {Address_Line1 : "XXX", Address_Line2 :"YYY", Name : "ZZZ"};
var hasKeyRegex = Object.keys(obj).some(function(key) {
return /Address/.test(key);
});
console.log(hasKeyRegex);