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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:33:48+00:00 2026-05-16T00:33:48+00:00

In JavaScript, all Objects act a bit like hashmaps. However, the keys to these

  • 0

In JavaScript, all Objects act a bit like hashmaps. However, the keys to these hashmaps must be strings. If they’re not, they’re converted with toString(). That means:

var a = {foo: 1};
var b = {bar: 2};
var o = {};
o[a] = 100;
o[b];              // 100
JSON.stringify(o); // '{"[object Object]":100}'

That is, since the toString() of any plain Object is [object Object], they all address the same value.

I’d like to create a hashmap where Objects with the same properties and values address the same value, but objects with different properties or values address different values. That is:

var a = {foo: 1};
var b = {bar: 2, baz: 3};
var c = {baz: 3, bar: 2};
var hash = new Hash();
hash.set(a, 100);
hash.get(b);      // undefined
hash.set(b, 200);
hash.get(b);      // 200
hash.get(c);      // 200

My first instinct was to use JSON.stringify() to turn objects into strings, but:

var hash = {};
var b = {bar: 2, baz: 3};
var c = {baz: 3, bar: 2};
hash[JSON.stringify(b)] = 100
hash[JSON.stringify(b)] // 100
hash[JSON.stringify(c)] // undefined
JSON.stringify(b)       // '{"bar":2,"baz":3}'
JSON.stringify(c)       // '{"baz":3,"bar":2}'

That is, JSON serialization is order-dependent.

Is there a good library or technique to implement a hashmap like this?

Update:

Equivalently, is there a good hashing function such that:

hash({foo: 1, bar: 2}) == hash({bar: 2, foo: 1})
  • 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-16T00:33:49+00:00Added an answer on May 16, 2026 at 12:33 am

    Here’s a quick proof-of-concept…

    I’ve hardly tested it at all, and I’m certain that there will be corner-cases that it can’t deal with.

    Performance will be hideously inefficient because the __createHash function needs to recurse through the members of any objects and then sort them, in order to generate a “hash” that meets your requirements.

    HashMap = function() {
        this.get = function(key) {
            var hash = this.__createHash(key);
            return this.__map[hash];
        };
    
        this.set = function(key, value) {
            var hash = this.__createHash(key);
            this.__map[hash] = value;
        };
    
        this.__createHash = function(key) {
            switch (typeof key) {
                case 'function':
                    return 'function';
    
                case 'undefined':
                    return 'undefined';
    
                case 'string':
                    return '"' + key.replace('"', '""') + '"';
    
                case 'object':
                    if (!key) {
                        return 'null';
                    }
    
                    switch (Object.prototype.toString.apply(key)) {
                        case '[object Array]':
                            var elements = [];
                            for (var i = 0; i < key.length; i++) {
                                elements.push(this.__createHash(key[i]));
                            }
                            return '[' + elements.join(',') + ']';
    
                        case '[object Date]':
                            return '#' + key.getUTCFullYear().toString()
                                       + (key.getUTCMonth() + 1).toString()
                                       + key.getUTCDate().toString()
                                       + key.getUTCHours().toString()
                                       + key.getUTCMinutes().toString()
                                       + key.getUTCSeconds().toString() + '#';
    
                        default:
                            var members = [];
                            for (var m in key) {
                                members.push(m + '=' + this.__createHash(key[m]));
                            }
                            members.sort();
                            return '{' + members.join(',') + '}';
                    }
    
                default:
                    return key.toString();
            }
        };
    
        this.__map = {};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction We all know these silly arguments objects of JavaScript functions. But why object?
i have an array of objects, and these objects all have an 'isvalid' attribute.
There is this tweet on Twitter : In JavaScript, all objects are truthy (as
Possible Duplicate: Get list of all input objects using JavaScript, without accessing a form
I'm a bit confused in how functions operate in javascript. I understand that they're
I'm trying to dynamically alter all objects in Javascript so that its construction can
In javascript, is there a way to prototype all objects. A simple usecase would
I´ve a Javascript object with all the data that I need to store on
All i want is to pass a HTML (DOM object) from javascript to Actionscript.
Hallo all. I got a javascript object with some propeties let's say function Animal()

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.