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 773441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:57:55+00:00 2026-05-14T18:57:55+00:00

I want to create a hashtable of json objects, where each json object represents

  • 0

I want to create a hashtable of json objects, where each json object represents a user.

I want to do this to sort of create a client side ‘cache’ of users.

User { ID: 234, name: 'john', ..);

So I can then reference things like this:

if(userCache[userid] != null)
alert(userCache[userid].ID);

is this possible?

  • 1 1 Answer
  • 1 View
  • 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-14T18:57:56+00:00Added an answer on May 14, 2026 at 6:57 pm

    Javascript objects themselves are maps, so for instance:

    var userCache = {};
    userCache['john']     = {ID: 234, name: 'john', ... };
    userCache['mary']     = {ID: 567, name: 'mary', ... };
    userCache['douglas']  = {ID: 42,  name: 'douglas', ... };
    
    // Usage:
    var id = 'john';
    var user = userCache[id];
    if (user) alert(user.ID); // alerts "234"
    

    (It wasn’t clear to me whether your “userid” would be “john” or 234, so I went with “john” above, but you could use 234 if you preferred.)

    It’s up to the implementation how the keys are stored and whether the map is a hash map or some other structure, but I’ve done this with hundreds of objects and been perfectly happy with the performance, even on IE, which is one of the slower implementations (at the moment).

    This works because there are two ways to get at the properties of a Javascript object: Via dotted notation, and via bracketed notation. For example:

    var foo = {bar: 10};
    alert(foo.bar);              // alerts "10"
    alert(foo['bar']);           // alerts "10"
    alert(foo['b' + 'a' + 'r']); // alerts "10"
    s = "bar";
    alert(foo[b]);               // alerts "10"
    

    It may seem strange that this bracketed syntax for getting an object property by name is the same as getting an array element by index, but in fact, array indexes are object properties in Javascript. Property names are always strings (in theory), but auto-conversion occurs when you do things like user[234]. (And implementations are free to optimize-out the conversion if they can, provided the semantics are maintained.)

    Edit Some bits and pieces:

    Looping through the cache

    And if you want to loop through the cache (and based on your follow-up question, you do, so perhaps others reading this question will want to too):

    var key, user;
    for (key in userCache) {
        // `key` receives the name of each property in the cache, so "john",
        // "mary", "douglas"...
        user = userCache[key];
        alert(user.ID);
    }
    

    The keys are looped in no defined order, it varies by browser.

    Deleting from the cache

    Suppose you want to delete a property from the cache entirely:

    delete userCache['john'];
    

    Now there is no longer a “john” property in the object at all.

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

Sidebar

Related Questions

I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I just want to know if it is possible to create a hashtable in
I want to have an ArrayList where it contains HashTables. I create a Hashtable
I want to create a JList that contains entries of a Hashtable of String
I want create subdomains like this: domain.com/type/city An examples: domain.com/restaurants/new_york domain.com/hotels/new_york domain.com/restaurants/chicago I have
I want to create a hash table with Object keys that are not converted
i want create multiple search where statement $where_search is a multiple condition from post
I want create an application with animate button? how can i do? after click

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.