¿Cómo puedo buscar en una matriz para ver si el valor existe?¿Cómo encontrar el valor del objeto en la matriz 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);
});
}
Hasta ahora he intentado .is
, .has
, .getdata
y .inarray
, pero me está llegando a ninguna parte.
La llamada devuelve JSON: [{"fruittype":"special item"},{"fruittype":"blue"},{"fruittype":"red"}]
¿Cómo es su matriz? –