Amit
Amit

Reputation: 213

How to retrieve values from a javascript Map when we have an object as a key?

I am trying to retrieve value present in a map for a particular key in a Javascript Map object:

It looks like this:

let group = new Map();
group = [
  [
    {
      "name": "Email",
      "displayText": "Email",
      "description": "xyz"
    },
    [
      {
        "name": "GoogleGmail",
        "displayText": "Gmail",
        "description": "xyz",
        "soln": "Email"
      }
    ]
  ],
  [
    {
      "name": "Documents",
      "displayText": "Documents",
      "description": "xyz"
    },
    []
  ],
  [
    {
      "name": "Files",
      "displayText": "Files",
      "description": "xyz"
    },
    []
  ]
]

console.log(group.get({
      "name": "Email",
      "displayText": "Email",
      "description": "xyz"
    }));

when I do this I get undefined. So how do we access the value

[
      {
        "name": "GoogleGmail",
        "displayText": "Gmail",
        "description": "xyz",
        "soln": "Email"
      }
]

Upvotes: 0

Views: 278

Answers (3)

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

The group variable is assigned a value of an array, and not a map. Here is a breakdown of your code.

Statement 1 (assigns a map)

let group = new Map();

Statement 2 (assigns an array, overrides the previous assignment)

group = [
.
.

];

Edit:
On the other hand, based on one of the comments in the question, you could do this below to get what you want. But really, it is unconventional to use objects as keys. See code snippet below:

let obj = {
      "name": "Email",
      "displayText": "Email",
      "description": "xyz"
    };


let group = new Map( [
  [
    obj,
    [
      {
        "name": "GoogleGmail",
        "displayText": "Gmail",
        "description": "xyz",
        "soln": "Email"
      }
    ]
  ],
  [
    {
      "name": "Documents",
      "displayText": "Documents",
      "description": "xyz"
    },
    []
  ],
  [
    {
      "name": "Files",
      "displayText": "Files",
      "description": "xyz"
    },
    []
  ]
]);

console.log(group.get(obj));

you would have to use the same object, not an object looking the same, it's generally ill-advised using objects as keys, if you have to, you should consider stringifying them first – Krzysztof Krzeszewski

Upvotes: 3

Zmen Hu
Zmen Hu

Reputation: 805

General speaking, its possible to use Object as key. However, you have to make sure that you are using the original object as key to get the value, not by another object that looks the same.

const group = new Map();
const o = { name: 'email' };
const k = { name: 'GoogleEmail'};
group.set(o, k);
group.get(o); // { name: 'GoogleEmail' }
groups.get({ name: 'email' }); // undefined

Besides, it's not recommend to use an object as the key. If you have to, use WeakMap instead, which has better performance:

const group = new WeakMap();
const o = { name: 'email' };
const k = { name: 'GoogleEmail'};
group.set(o, k);
group.get(o); // { name: 'GoogleEmail' }

Upvotes: 0

Rilla
Rilla

Reputation: 534

From the code above group is not map it's an array

Below a example of how to set and get data from a map;

let group = new Map();

group.set('a', 'foo');

console.log(group.get('a')) // would log 'foo'

To use a an object as the key of a map u have to store a reference to the object so you can get the value of that object using it as a reference Example:

let a = {a: 1} 

let group = new Map();

group.set(a, 'foo')

console.log(group.get(a)); // would log 'foo'

Upvotes: 2

Related Questions