Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:38+00:00 2026-05-22T20:49:38+00:00

Take two :) I have a jquery’s autocomplete texbox and, upon a click of

  • 0

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"
    }
]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:49:39+00:00Added an answer on May 22, 2026 at 8:49 pm

    (Replacement answer, I hadn’t meant the last one to be a CW.)

    The problems is that ‘cache’ is some sort of an array of JSON objects

    Actually, it’s not an array. It’s an object. (It’s created via var cache = {};, which is how we can tell.)

    …but to be able to use if ( input in cache ) { ... } I need to convert it to a simple javascript array.

    Actually, no. The in operator in this context tests to see if an object contains a property with the name you give it. Example:

    var obj = {foo: 1};  // An object with a property called "foo"
    alert('foo' in obj); // alerts true, `obj` has a "foo" property
    alert('bar' in obj); // alerts false, there is no "bar" property in `obj`
    

    Note that the left-hand side of the in operator 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 click handler is taking the value of the “inputV” field and seeing if that value is the name of a property in the cache object.

    You probably want to check if it’s the value of one of the properties of the cache object. If so:

    $('#btn_check').click(function()        {
    
        var input = $("#inputV").val();
        var found, propName;
    
        alert(input);
    
        console.log('cache='+cache); 
        /// FireBug says 'cache=[object Object]'
    
        found = false;
        for (propName in cache ) {
            if (cache[propName] == input) {
                found = true;
                break;
            }
        }
        alert(found);
    });
    

    Since we know cache is a boring old object (from the fact it’s created with var cache = {};), we can pretty safely use a raw for..in loop as above. But if you wanted to be more careful, you would use hasOwnProperty to make sure that you only checked propeties that cache has its own copy of (as opposed to ones it inherits from its prototype):

        for (propName in cache ) {
            if (cache.hasOwnProperty(propName) && cache[propName] == input) {
                found = true;
                break;
            }
        }
    

    Again, though, it’s not really necessary in this case, because we know that cache is a plain object, and barring someone doing something really silly like extending Object.prototype (which Thou Shalt Not Do), all of its enumerable properties (the things that for..in enumerates) are its own properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1:
I have two ICollection s of which I would like to take the union.
I have the opportunity to take a two day class on Perl 6 with
I have two Python functions, both of which take variable arguments in their function
When Using UIPickerController I have two choices on my App: take a picture take
I have two expressions of type Expression<Func<T, bool>> and I want to take to
I have a container div that holds two internal divs; both should take 100%
For example if I have an Enum with two cases, does it make take
I have two ordered lists next to each other. When I take a node
I have a LoadingStatus Function that has two options SHOW or HIDE. The Show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.