javascript逻辑中的小问题

时间:2017-03-17 09:20:36

标签: javascript ajax arrayobject

我有一个数组对象

var data.responseText=[{"_id":"xyz","stat":"true"},{"_id":"xyz","stat":"true"},
{"_id":"xyz","stat":"false"}]

如果有任何带有“stat”的数组:“false”,我需要检查这个数组对象。如果它发现我将控制它。如果不是,我将使该数组为null。 另外一件事是在一个功能中,如何在2分钟的计时器之后调用此函数。

function getdata(){
return $.ajax({
type:"GET",
url:"url"**
});
}//how to call this function after 2 mins
var data.responseText=getdata;
//after getting that data how can I search for that above mentioned

有人可以提供帮助 !!

2 个答案:

答案 0 :(得分:0)

您可以使用filter获取stat值为false的数组。 filter将返回一个新数组。检查阵列的长度。如果它大于0则有一个stat  其值为false,否则没有具有该值的键

var responseText = [{
  "_id": "xyz",
  "stat": "true"
}, {
  "_id": "xyz",
  "stat": "true"
}, {
  "_id": "xyz",
  "stat": "false"
}]

var haveFalse = (responseText).filter(function(item,index){
   return item['stat'] === 'false'


})
console.log(haveFalse.length)

如果您尚未定义data,则var data.responseText将引发错误

答案 1 :(得分:0)

下面都包括在内,一个是作为参考的3秒计时器,另一个是检查false并将所有其他元素更改为null。将这些逻辑构建到您的代码中以实现目标,如有疑问,请发表评论

var responseText=[{"_id":"xyz","stat":"true"},{"_id":"abc","stat":"true"},
{"_id":"def","stat":"false"}];

setTimeout(function(){
$(responseText).each(function(x,y){
if(y["stat"]=="false"){
console.log(y);
}else{
responseText[x]=null;
}
}); console.log(responseText);},3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>