Ci-dessous mon extrait de code, comment puis-je vérifier dans le tableau des objets json s'il y a un "état" de "DÉCONNECTÉ"?

J'ai essayé,

if(data[].status["DISCONNECTED"].length){
    console.log(true);
}

Mais ne semble pas fonctionner, des idées, de l'aide, des indices, des suggestions, des recommandations s'il vous plaît?

$(document).ready(function(){
  var data = [{"b_id":1,"name":"t1","status":"CONNECTED"},{"b_id":3,"name":"t2","status":"DISCONNECTED"},{"b_id":4,"name":"t3","status":"CONNECTED"},{"b_id":5,"name":"t4","status":"CONNECTED"},{"b_id":6,"name":"t5","status":"CONNECTED"}];
  
  $.each(data,function(index,value){
    console.log(value.status);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
-2
Juliver Galleto 8 mars 2016 à 05:32

5 réponses

Meilleure réponse

Si vous utilisez lodash (ou si cela ne vous dérange pas de l'inclure), vous pouvez utiliser sa méthode .where:

var data = [{"b_id":1,"name":"t1","status":"CONNECTED"},{"b_id":3,"name":"t2","status":"DISCONNECTED"},{"b_id":4,"name":"t3","status":"CONNECTED"},{"b_id":5,"name":"t4","status":"CONNECTED"},{"b_id":6,"name":"t5","status":"CONNECTED"}];

if (_.where(data, {status: 'DISCONNECTED'}).length) {
    console.log('exitsts');
}

Ou sans bibliothèque, en utilisant Array.prototype.some()

var a = data.some(function(val) {
    return val.status == 'DISCONNECTED';
});

if (a) {
    console.log('exists');
}
1
baao 8 mars 2016 à 02:54

Ouais, votre code semble bien fonctionner. quel est le problème?

   $(document).ready(function(){
  var data = [{"b_id":1,"name":"t1","status":"CONNECTED"},{"b_id":3,"name":"t2","status":"DISCONNECTED"},{"b_id":4,"name":"t3","status":"CONNECTED"},{"b_id":5,"name":"t4","status":"CONNECTED"},{"b_id":6,"name":"t5","status":"CONNECTED"}];
  
  $.each(data,function(index,value){

if (value.status == "DISCONNECTED"){
	// DISCONNECTED founde!!
	console.log(value.status);
}
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
0
wan rui 8 mars 2016 à 02:42
$(document).ready(function() {
  var data = [{
    "b_id": 1,
    "name": "t1",
    "status": "CONNECTED"
  }, {
    "b_id": 3,
    "name": "t2",
    "status": "DISCONNECTED"
  }, {
    "b_id": 4,
    "name": "t3",
    "status": "CONNECTED"
  }, {
    "b_id": 5,
    "name": "t4",
    "status": "CONNECTED"
  }, {
    "b_id": 6,
    "name": "t5",
    "status": "CONNECTED"
  }];

  $.each(data, function(index, value) {
    if (value.status == "DISCONNECTED") {
      console.log(value.status);
    }

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Cela ne ferait-il pas l'affaire?

0
guradio 8 mars 2016 à 02:39
$(document).ready(function(){
  var data = [{"b_id":1,"name":"t1","status":"CONNECTED"},{"b_id":3,"name":"t2","status":"DISCONNECTED"},{"b_id":4,"name":"t3","status":"CONNECTED"},{"b_id":5,"name":"t4","status":"CONNECTED"},{"b_id":6,"name":"t5","status":"CONNECTED"}];
  
  $.each(data,function(index,value){
    var newObj = value;
    alert(newObj.status);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
0
Ivan Karaman 8 mars 2016 à 02:42
for(var i=0;i<arr.length;i++){
    var obj = arr[i];
    for(var key in obj){
        var attrName = key;
        var attrValue = obj[key];
    }
}
-1
Thomas Bormans 8 mars 2016 à 14:53