Come posso cercare in una matrice per vedere se il valore esiste?Come trovare il valore dell'oggetto nella matrice con Jquery?
var fruitVarietyChecked = $('input[name=fruitVariety]:checked').val();
$.getJSON('getdata.php', {fruitVariety: fruitVarietyChecked}, function(fruittype) {
var html = '';
$.each(fruittype, function(index, array) {
alert("Key: " + index + ", Value: " + array['fruittype']);
//shows array - Key: 0 , Value: special item
//this is where the problem is
if ($(array.has("special item"))){
$("p").text("special item" + " found at " + index);
return false;
}
html = html + '<label><input type="radio" name="fruitType" value="' + array['fruittype'] + '" />' + array['fruittype'] + '</label> ';
});
$('#fruittype').html(html);
});
}
Finora ho provato .is
, .has
, .getdata
e .inarray
, ma mi sta andando da nessuna parte.
La chiamata JSON restituisce: [{"fruittype":"special item"},{"fruittype":"blue"},{"fruittype":"red"}]
Che aspetto ha il tuo array? –