Dynamically add data to a javascript map

284,503

Solution 1

Well any Javascript object functions sort-of like a "map"

randomObject['hello'] = 'world';

Typically people build simple objects for the purpose:

var myMap = {};

// ...

myMap[newKey] = newValue;

edit — well the problem with having an explicit "put" function is that you'd then have to go to pains to avoid having the function itself look like part of the map. It's not really a Javascripty thing to do.

13 Feb 2014 — modern JavaScript has facilities for creating object properties that aren't enumerable, and it's pretty easy to do. However, it's still the case that a "put" property, enumerable or not, would claim the property name "put" and make it unavailable. That is, there's still only one namespace per object.

Solution 2

Javascript now has a specific built in object called Map, you can call as follows :

   var myMap = new Map()

You can update it with .set :

   myMap.set("key0","value")

This has the advantage of methods you can use to handle look ups, like the boolean .has

  myMap.has("key1"); // evaluates to false 

You can use this before calling .get on your Map object to handle looking up non-existent keys

Share:
284,503

Related videos on Youtube

stevebot
Author by

stevebot

http://www.codedforyou.com http://techmobilehub.blogger.com

Updated on December 14, 2021

Comments

  • stevebot
    stevebot over 2 years

    Is there a way I can dynamically add data to a map in javascript. A map.put(key,value)? I am using the yui libraries for javascript, but didn't see anything there to support this.

  • stevebot
    stevebot about 14 years
    Right, I mean a javascript "map". how would you build a myMap.put() function?
  • Matti Virkkunen
    Matti Virkkunen about 14 years
    @stevebot: Doesn't the last line in his post do exactly that?
  • stevebot
    stevebot about 14 years
    Okay, I hear you. I can just do myMap[anyKey] = anyValue and this works for me. thanks!
  • master_dodo
    master_dodo over 7 years
    How to put/add in the beginning of the map?
  • Pointy
    Pointy over 7 years
    @user3241111 maps don't have a "beginning"
  • master_dodo
    master_dodo over 7 years
    @Pointy So, currently I am doing like this: mapA having ele1, ele2, ele3, and I've to add ele4 in the beginning. I am creating another map(mapB) having just ele4 and then creating 3rd map which is actually using mapB and mapA is the same order (not mapA and mapB).. I don't know if map has order or not but while iterating they always give the same order of elements, which makes me doubt if they really don't have order.
  • Pointy
    Pointy over 7 years
    @user3241111 object properties have no guaranteed order when you iterate with for ... in or Object.keys(). JavaScript runtimes don't randomize property order for no reason, but it's risky to write code that relies on the ordering remaining consistent because it can definitely change if properties are added/removed. You can always keep your own array of property keys and maintain its order however you want, and then use it to iterate through property values as necessary.
  • Akavall
    Akavall almost 6 years
    An additional note: myMap["key0"] = "value" is NOT an alternative syntax for myMap.set("key0","value"), it "works", but probably does something most poeple are not looking for.
  • pwilcox
    pwilcox over 5 years
    @Akavall, one hour trying to figure out why Map.delete doesn't work and why it's size shows 0 even though the console output clearly shows the items are in there and I realize I put in the items just as you've mentioned. As you said, it 'works', but it's not what you want.
  • yue you
    yue you over 4 years
    This is a proper map object in javascript. Please vote this