107

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.

0

4 Answers 4

161

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.

7
  • 1
    Right, I mean a javascript "map". how would you build a myMap.put() function?
    – stevebot
    Commented Jun 3, 2010 at 14:49
  • 2
    @stevebot: Doesn't the last line in his post do exactly that? Commented Jun 3, 2010 at 14:58
  • Okay, I hear you. I can just do myMap[anyKey] = anyValue and this works for me. thanks!
    – stevebot
    Commented Jun 3, 2010 at 15:06
  • How to put/add in the beginning of the map? Commented Dec 1, 2016 at 10:00
  • 2
    @user3241111 maps don't have a "beginning"
    – Pointy
    Commented Dec 1, 2016 at 14:01
86

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

4
  • 15
    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.
    – Akavall
    Commented Sep 12, 2018 at 5:25
  • 6
    @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.
    – pwilcox
    Commented Sep 30, 2018 at 23:51
  • 1
    This is a proper map object in javascript. Please vote this
    – yue you
    Commented Nov 26, 2019 at 19:47
  • how would you set a map of map? do you have to get one then set the sub map? i guess
    – mike01010
    Commented Sep 2, 2022 at 19:09
2

In Typescript

let ar = [1, 2, 3, 4, 5, 6];

 let map = new Map<number, string>();
    ar.forEach(value => {
      map.set(value, 'value'+ value);
    });
console.log(map, 'map data');

1

I like this way to achieve this

const M = new Map(Object.entries({ 
  language: "JavaScript" 
}));

console.log(M.size); // 1 
console.log(...M); // ["language", "JavaScript"]

// (1) Add and update some map entries 
M.set("year", 1991); 
M.set("language", "Python");

console.log(M.size); // 2 
console.log(...M); // \["language", "Python"\] ["year", 1991]

Not the answer you're looking for? Browse other questions tagged or ask your own question.