我正在尝试检查对象数组中是否存在值
function hasProperties(id){
jQuery(JSON.parse(jQuery("#PropertiesField").html())).each(function () {
if(id== jQuery(this)[0].properties.id) {
console.log((id== jQuery(this)[0].properties.id));
return "Present";
}
})
};
var something = hasProperties("someid");
上面的代码段返回,但在控制台中也记录undefined
为something
true。为什么present
当条件满足时不返回,我做错了什么?
每个方法中提供的函数是一个匿名内部函数。因此,在each()上下文之外不返回任何内容。为了解决这个问题,您可以执行类似的操作,
function getProperty(id){
var result;
$('your element').each(function(){
//If your condition is true
result=expectedValue
});
return result;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句