检查 Javascript 对象是否具有以特定字符串开头的属性名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27507110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a Javascript Object has a property name that starts with a specific string
提问by Alexander Rutz
Lets say I have javascript objects like this one:
假设我有这样的 javascript 对象:
addr:housenumber: "7"
addr:street: "Frauenplan"
owner: "Knaut, Kaufmann"
How can I check if the object has a property name that starts with addr? I'd imagine something along the lines of the following shouldbe possible:
如何检查对象是否具有以 开头的属性名称addr?我想应该是可能的:
if (e.data[addr*].length) {
I tried RegExpand .match()to no avail.
我尝试过RegExp,.match()但无济于事。
回答by Amit Joki
You can check it against the Object's keys using Array.somewhich returns a bool.
您可以使用Array.some返回 a的对象的键来检查它bool。
if(Object.keys(obj).some(function(k){ return ~k.indexOf("addr") })){
// it has addr property
}
You could also use Array.filterand check it's length. But Array.someis more apt here.
您也可以使用Array.filter并检查它的长度。但Array.some这里更贴切。
回答by Ethan Lynn
You can use the Object.keysfunction to get an array of keys and then use the filtermethod to select only keys beginning with "addr".
您可以使用该Object.keys函数获取一组键,然后使用该filter方法仅选择以 开头的键"addr"。
var propertyNames = Object.keys({
"addr:housenumber": "7",
"addr:street": "Frauenplan",
"owner": "Knaut, Kaufmann"
}).filter(function (propertyName) {
return propertyName.indexOf("addr") === 0;
});
// ==> ["addr:housenumber", "addr:street"];
This gives you existence (propertyNames.length > 0) and the specific names of the keys, but if you just need to test for existence you can just replace filterwith some.
这为您提供了存在 ( propertyNames.length > 0) 和键的特定名称,但是如果您只需要测试是否存在,您可以将其替换filter为some.
回答by Edwin Reynoso
Obj = {address: 'ok', x:5}
Object.keys(obj).some(function(prop){
return ~prop.indexOf('add')
}) //true
回答by Spock
I don't think "Querying" a object properties is possible. You would have to loop through the properties individually and determine if they match. something like this...
我认为“查询”对象属性是不可能的。您必须逐个遍历属性并确定它们是否匹配。像这样的东西......
function findProp(e, prop)
{
for(var o in e.data){
if(o.substr(0, prop.length)==prop) //<- you could try the regex or match operation here
return true;
}
return false;
}
alert(findProp({data:{addr:{street:"a", sub:"b"}}}, 'addr'));
You can then process the property if it findProp returns true...
如果 findProp 返回 true,则您可以处理该属性...
回答by Mahib
You can write a function
你可以写一个函数
function check(model) {
var keys = [];
for (key in model) {
if(key.indexOf("addr") > -1)
keys.push(key);
}
return keys;
}
回答by Krzysztof Trzos
Try this:
尝试这个:
var myObject = {
'prop1': 'value1',
'xxxxx': 'value2'
};
var stringToCheck = 'addr';
for(var propertyName in myObject) {
var x = propertyName.substr(0, stringToCheck.length - 1);
if(x == stringToCheck) {
return true;
}
}
回答by Bwyss
Why not just
为什么不只是
var foo = {'bar':'funtimes'};
if (foo.bar.includes('fun')) {
console.log('match');
}

