1

I did some research and it seems that we can either use Jquery.map() or create a javascript object: var Map = {}; But both of them don't seem to provide rich functionalities to easily manipulate a map.

The MDN seems to have a Map type, but it is only supported by IE11.
Is there a Map type in javascript similar to what is in Java that provides map functionality, ex, add an element, iterate thru the map, things like that ? Thanks!!

Rune FS
  • 21,497
  • 7
  • 62
  • 96
jlp
  • 1,656
  • 6
  • 33
  • 48
  • All Javascript objects are like maps themselves. See http://stackoverflow.com/questions/8312459/iterate-through-object-properties – aa333 Mar 17 '15 at 20:47
  • 3
    It is [coming](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), you can use [es6 shim](https://github.com/paulmillr/es6-shim/) – aarosil Mar 17 '15 at 20:49
  • 1
    What are you trying to do? Javascript is a very different language than Java, and there may be a way to do what you want easily that your question is preventing from being asked. – James Black Mar 17 '15 at 20:58
  • true, if you could be more specific. Normally var Map = {} is enough. The problem with this solution is that the methods do not come attached, but they are part of the language(access properties on objects), that might be what is confusing you. – Victor Mar 17 '15 at 21:02

2 Answers2

3

You ca use common objects, depending on what you want.

var thing = {property:"value", otherProperty:"othervalue"};

They are really flexible if you go trough the JavaScript api and language you can add,delete and access, from objects, which kind of looks like a map, but without all the methods attached, those that you would find on Java Maps.

Besides that they can help a lot, if you are not looking for hashmap<>s or treemap<>s

The problem with this solution is that the methods do not come attached, as in a class, but they are part of the language(access properties on objects), that might be what is confusing you.

Some links that might be useful (they are a bit of basic stuff, but if it can help someone...):

How to create a simple map using JavaScript/JQuery

JavaScript property access: dot notation vs. brackets?

http://www.w3schools.com/js/js_properties.asp

http://www.sitepoint.com/back-to-basics-javascript-object-syntax/

An interresting quote (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map):

Objects and maps compared

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Because of this, Objects have been used as Maps historically; however, there are important differences between Objects and Maps that make using a Map better.

An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = Object.create(null). The keys of an Object are Strings, where they can be any value for a Map. You can get the size of a Map easily while you have to manually keep track of size for an Object. Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.

An example (How to create a simple map using JavaScript/JQuery):

var map = new Object(); // or var map = {};
map[myKey1] = myObj1;
map[myKey2] = myObj2;

function get(k) {
   return map[k];
}

PS if you want to use Map("This is an experimental technology"). Objects are everywhere and are standardized for all browsers.

Community
  • 1
  • 1
Victor
  • 3,520
  • 3
  • 38
  • 58
1

You could create your own.

Here is a starting point:

var Map = (function() {

  return function() {
    var hashMap = {};
    var hashOrder = [];

    return {
      get: function(key) {
        if (hashMap[key]) {
          return hashMap[key];
        }
      },
      has: function(key) {
        return hashOrder.indexOf(key) > -1;
      },
      set: function(key, value) {
        if (this.has(key)) {
          throw new Error('Cannot set a key that already exists!');
        }
        hashOrder.push(key);
        return hashMap[key] = value;
      },
      remove: function(key) {
        var index = hashOrder.indexOf(key);
        if (index > -1) {
          hashOrder.splice(index, 1);
        }
        return delete hashMap[key];
      },
      values: function() {
        var arr = [];
        this.each(function(value) {
          arr.push(value);
        });
        return arr;
      },
      keys: function() {
        return hashOrder;
      },
      each: function(fn) {
        var key;
        var i = 0;
        var length = hashOrder.length;
        for (i; i < length; ++i) {
          key = hashOrder[i];
          fn(hashMap[key], key);
        }
      },
      toJSON: function() {
        var jsonString = JSON.stringify(hashMap);

        return JSON.parse(jsonString);
      },
      toString: function() {
        return JSON.stringify(hashMap);
      }
        //..etc
    };
  };
})();

// then you can do:


var map = new Map();
map.set('lemons', 12);
map.set('peanuts', 42);
console.log(map.values(), map.keys());
map.each(function(value, key) {
  console.log('EACH', value, key);
});
console.log(map.toJSON(), map + '');
Naftali
  • 144,921
  • 39
  • 244
  • 303