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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:01:59+00:00 2026-06-05T05:01:59+00:00

I want to create a hash table with Object keys that are not converted

  • 0

I want to create a hash table with Object keys that are not converted into String.

Some thing like this:

var object1 = new Object();
var object2 = new Object();

var myHash = new HashTable();

myHash.put(object1, "value1");
myHash.put(object2, "value2");

alert(myHash.get(object1), myHash.get(object2)); // I wish that it will print value1 value2

EDIT: See my answer for full solution

  • 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-06-05T05:02:01+00:00Added an answer on June 5, 2026 at 5:02 am

    Here is a proposal:

    function HashTable() {
        this.hashes = {};
    }
    
    HashTable.prototype = {
        constructor: HashTable,
    
        put: function( key, value ) {
            this.hashes[ JSON.stringify( key ) ] = value;
        },
    
        get: function( key ) {
            return this.hashes[ JSON.stringify( key ) ];
        }
    };
    

    The API is exactly as shown in your question.

    You can’t play with the reference in js however (so two empty objects will look like the same to the hashtable), because you have no way to get it. See this answer for more details: How to get javascript object references or reference count?

    Jsfiddle demo: http://jsfiddle.net/HKz3e/

    However, for the unique side of things, you could play with the original objects, like in this way:

    function HashTable() {
        this.hashes = {},
        this.id = 0;
    }
    
    HashTable.prototype = {
        constructor: HashTable,
    
        put: function( obj, value ) {
            obj.id = this.id;
            this.hashes[ this.id ] = value;
            this.id++;
        },
    
        get: function( obj ) {
            return this.hashes[ obj.id ];
        }
    };
    

    Jsfiddle demo: http://jsfiddle.net/HKz3e/2/

    This means that your objects need to have a property named id that you won’t use elsewhere. If you want to have this property as non-enumerable, I suggest you take a look at defineProperty (it’s not cross-browser however, even with ES5-Shim, it doesn’t work in IE7).

    It also means you are limited on the number of items you can store in this hashtable. Limited to 253, that is.

    And now, the “it’s not going to work anywhere” solution: use ES6 WeakMaps. They are done exactly for this purpose: having objects as keys. I suggest you read MDN for more information: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/WeakMap

    It slightly differs from your API though (it’s set and not put):

    var myMap = new WeakMap(),
        object1 = {},
        object2 = {};
    
    myMap.set( object1, 'value1' );
    myMap.set( object2, 'value2' );
    
    console.log( myMap.get( object1 ) ); // "value1"
    console.log( myMap.get( object2 ) ); // "value2"
    

    Jsfiddle demo with a weakmap shim: http://jsfiddle.net/Ralt/HKz3e/9/

    However, weakmaps are implemented in FF and Chrome (only if you enable the “Experimental javascript features” flag in chrome however). There are shims available, like this one: https://gist.github.com/1269991. Use at your own risk.

    You can also use Maps, they may more suit your needs, since you also need to store primitive values (strings) as keys. Doc, Shim.

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

Sidebar

Related Questions

I want to create a hash table that relies on an independent vector data
Say I have some known values, against which I want to create a hash
How can I create a hash table object in JavaSript and use a date
I want to create a JList that contains entries of a Hashtable of String
I want to generate code for creating a hash table object and assigning it
I want to create a static Hash table to convert strings to integers .
i want create a counter that retrieve the number of month, day, hour, minute
I want create an array in JSF EL. How can I do that? Is
I want create a new JavaScript object based on an existing JavaScript object. Till
I want create module which update list of usb devices automatically (not only mass

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.