Take two 🙂 I have a jquery’s autocomplete texbox and, upon a click of a button, I need to check if the value entered has come from the autocomplete or is it a completely new value.
The problems is that ‘cache’ is some sort of an array of JSON objects, but to be able to use if ( input in cache ) { … } I need to convert it to a simple javascript array. What would be the best way to do that?
P.S. FireBug says ‘cache=[object Object]’
//////////////////////////////////////////// autocomplete code //////////////////
var cache = {},
lastXhr;
$( "#inputV" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
cache[ term ] = data;
if ( xhr === lastXhr ) {
response( data );
}
});
}
});
////////////////////////// check if input comes from autocomplete //////////////////
$('#btn_check').click(function() {
var input = $("#inputV").val();
alert(input);
console.log('cache='+cache);
/// FireBug says 'cache=[object Object]'
if ( input in cache )
{
alert("yes");
}
else
{
alert("no");
}
});
Here’s that a response looks like.
[
{
"id": "Podiceps nigricollis",
"label": "Black-necked Grebe",
"value": "Black-necked Grebe"
},
{
"id": "Nycticorax nycticorax",
"label": "Black-crowned Night Heron",
"value": "Black-crowned Night Heron"
},
{
"id": "Tetrao tetrix",
"label": "Black Grouse",
"value": "Black Grouse"
},
{
"id": "Limosa limosa",
"label": "Black-tailed Godwit",
"value": "Black-tailed Godwit"
},
{
"id": "Chlidonias niger",
"label": "Black Tern",
"value": "Black Tern"
},
{
"id": "Larus marinus",
"label": "Great Black-backed Gull",
"value": "Great Black-backed Gull"
},
{
"id": "Larus fuscus",
"label": "Lesser Black-backed Gull",
"value": "Lesser Black-backed Gull"
},
{
"id": "Larus ridibundus",
"label": "Black-headed Gull",
"value": "Black-headed Gull"
},
{
"id": "Turdus merula",
"label": "Common Blackbird",
"value": "Common Blackbird"
},
{
"id": "Sylvia atricapilla",
"label": "Blackcap",
"value": "Blackcap"
},
{
"id": "Rissa tridactyla",
"label": "Black-legged Kittiwake",
"value": "Black-legged Kittiwake"
},
{
"id": "Aegypius monachus",
"label": "Eurasian Black Vulture",
"value": "Eurasian Black Vulture"
}
]
(Replacement answer, I hadn’t meant the last one to be a CW.)
Actually, it’s not an array. It’s an object. (It’s created via
var cache = {};, which is how we can tell.)Actually, no. The
inoperator in this context tests to see if an object contains a property with the name you give it. Example:Note that the left-hand side of the
inoperator must be a string (it can be a literal, as above, or any expression that results in a string, such as a variable reference).The code in your
clickhandler is taking the value of the “inputV” field and seeing if that value is the name of a property in thecacheobject.You probably want to check if it’s the value of one of the properties of the
cacheobject. If so:Since we know
cacheis a boring old object (from the fact it’s created withvar cache = {};), we can pretty safely use a rawfor..inloop as above. But if you wanted to be more careful, you would usehasOwnPropertyto make sure that you only checked propeties thatcachehas its own copy of (as opposed to ones it inherits from its prototype):Again, though, it’s not really necessary in this case, because we know that
cacheis a plain object, and barring someone doing something really silly like extendingObject.prototype(which Thou Shalt Not Do), all of its enumerable properties (the things thatfor..inenumerates) are its own properties.