简体   繁体   中英

Small issue in javascript logic

I have an array object as

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

I need check in this array object if there is any array with "stat":"false".If it finds I will console that.If not I will make that array to null. One more thing this whole is in one fuction and how can I call this function after a timer of 2mins.

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

Can someone help !!

You can use filter to get an array where the stat value is false. filter will return a new array. Check the length of the array. If it is more than 0 then there is a stat whose value is false, else there is no such key with that value

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)

If you have not defined data then var data.responseText will throw an error

Below are both included , one is the Timer for 3 seconds as a reference, and the other for the check for false and changing all othe elements to null. Build both these logics into your code to achieve the goal , in case of further doubt, please comment

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM