jQuery 判断对象在javascript中是否有属性和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19301768/
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
Determine if the object has a property and value in javascript
提问by Joey Hipolito
I wanted to check if the an object has a property of something and its value is equal to a certain value.
我想检查一个对象是否具有某个属性,并且它的值是否等于某个值。
var test = [{name : "joey", age: 15}, {name: "hell", age: 12}]
There you go, an array of objects, now I wanted to search inside the object and return true if the object contains what I wanted.
好了,对象数组,现在我想在对象内部进行搜索,如果对象包含我想要的内容,则返回 true。
I tried to do it like this:
我试着这样做:
Object.prototype.inObject = function(key, value) {
if (this.hasOwnProperty(key) && this[key] === value) {
return true
};
return false;
};
This works, but not in an array. How do I do that?
这有效,但不在数组中。我怎么做?
回答by Bergi
Use the some
Array methodto test your function for each value of the array:
使用some
Array 方法针对数组的每个值测试您的函数:
function hasValue(obj, key, value) {
return obj.hasOwnProperty(key) && obj[key] === value;
}
var test = [{name : "joey", age: 15}, {name: "hell", age: 12}]
console.log(test.some(function(boy) { return hasValue(boy, "age", 12); }));
// => true - there is a twelve-year-old boy in the array
Btw, don't extend Object.prototype
.
顺便说一句,不要扩展Object.prototype
。
回答by bortunac
-- for the property --
——对于房产——
if(prop in Obj)
//or
Obj.hasOwnProperty(prop)
-- for the value ---
-- 为价值 --
Using "Object.prototype.hasValue = ..." will be FATAL for js but Object.definePropertylet you define properties with enumerable:false(default)
使用 "Object.prototype.hasValue = ..." 对 js 来说是致命的,但Object.defineProperty允许你使用enumerable:false(默认) 定义属性
Object.defineProperty(Object.prototype,"hasValue",{
value : function (obj){
var $=this;
for( prop in $ ){
if( $[prop] === obj ) return prop;
}
return false;
}
});
just for experiment test if a NodeList has an Element
仅用于实验测试 NodeList 是否具有 Element
var NL=document.QuerySelectorAll("[atr_name]"),
EL= document.getElementById("an_id");
console.log( NL.hasValue(EL) )
// if false then #an_id has not atr_name
回答by Snake Eyes
For array, of course you have to browse that array with for
对于数组,当然你必须浏览该数组 for
for(var i = 0 ; i < yourArray.length; i++){
if(yourArray[i].hasOwnProperty("name") && yourArray[i].name === "yourValue") {
//process if true
}
}
回答by Ali Hayder
Here is another solution for checking if the object has the property but the value of property is not set. Maybe the property value has 0, null or an empty string.
这是检查对象是否具有属性但未设置属性值的另一种解决方案。也许属性值有 0、null 或空字符串。
array.forEach(function(e){
if(e.hasOwnProperty(property) && Boolean(e[property])){
//do something
}
else{
//do something else
}
});
Boolean() is the trick here.
Boolean() 是这里的诀窍。
回答by Halcyon
Typically you'll use something like Object.first
:
通常你会使用类似的东西Object.first
:
// search for key "foo" with value "bar"
var found = !!Object.first(test, function (obj) {
return obj.hasOwnProperty("foo") && obj.foo === "bar";
});
Assuming that Object.first
will return some falsy value when it doesn't find a match.
假设Object.first
当它找不到匹配项时会返回一些假值。
Object.first
is not a native function but check on of the popular frameworks, they're bound to have one.
Object.first
不是本机功能,但检查流行的框架,他们一定会有一个。