0

I'm wondering if the following can be done another way in javascript/es6:

var myMap = new Map();
myMap.set("id1", {"name": "John", "address":"near"} );
myMap.set("id2", {"name": "Úna", "address":"far away"} );

myMap.set("id2", {"name": "Úna", "address":"near"} );

I'm new to javascript and have read in Map's documentation that Objects have historically been used as Maps. Can the above been done using objects? I really don't like that I have to use set and get either.

Baz
  • 12,713
  • 38
  • 145
  • 268

3 Answers3

4

As long as the key values are mere strings, you can just replace a Map with a plain object (You've already done that with the values you provided):

// init object
let myMap = {
  "id1": {"name": "John", "address":"near"},
  "id2": {"name": "Úna", "address":"far away"}
};

// add/replace
myMap.id2 = {"name": "Úna", "address":"near"};

// access
console.log( myMap.id2 );

The advantage of a Map is, that you can use arbitrary objects as a key value. In objects those would be converted to a string, which oftentimes does not result in a meaningful representation.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Is there an alternative for when keys are not mere strings? The issue with Map seems to be that keys are based on reference-equality as opposed to 'composition' equality. Think hashCodes. – Alexander Terp Apr 12 '23 at 16:35
  • 1
    @AlexanderTerp I asked almost the same thing and the answer in general seems to be "no": https://stackoverflow.com/q/31582852/1169798 – Sirko Apr 12 '23 at 21:31
1

Following code helps to search and push data to object like map. But does accept duplicate key not as per map

const object1 = {
};

function pushit(keyStr,valStr,objectData)
{
  objectData[keyStr] = valStr;
}
function searchPrint(keyStr)
{
  if(object1.hasOwnProperty(keyStr))
  {
  console.log(object1[keyStr]);
  }
}

pushit("sys msg" , "custom msg", object1);
pushit("sys msg1" , "custom msg1", object1);
pushit("sys msg2" , "custom msg2", object1);

searchPrint("sys msg2");

This is not an alternative for map but to store key and value and to search the same.

Parameshwar
  • 856
  • 8
  • 16
0

Of course you can, what you have is basically

let obj = {
    id1: {"name": "John", "address":"near"},
    id2: {"name": "Úna", "address":"far away"}
}

Note that, as well in the Map, and in the object too, the keys will override each other when setting a key twice. In strict mode, this would even be an error for the object.

baao
  • 71,625
  • 17
  • 143
  • 203
  • Setting a key twice is a error in strict mode @torazaburo – baao Apr 10 '17 at 08:52
  • Do you mean including the same key twice in a single object literal? I don't think that is an error. At least not when I try it. –  Apr 10 '17 at 08:54
  • Yeah, that's what I mean. You don't get an error because of this in es6: https://bugzilla.mozilla.org/show_bug.cgi?id=1041128 . It is also noted on mdn: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Strict_mode#Changes_in_strict_mode (it's the fourth part there) @torazaburo – baao Apr 10 '17 at 09:08