1

Does anyone knows how to directly call a array key of a Map Object.

As shown in below code, I can map.get(arr), but not map.get([0, 1, 2, 3])

const map = new Map() const arr = [0,1,2,3] map.set(arr, "I am some number") map.get(arr) // "I am some number" map.get([0,1,2,3]) // undefined

5
  • you create a new array which does not share the (same) object reference from the used key of the map. Commented Jan 17, 2020 at 9:00
  • map.keys() will return Iterator where you can achieve you arr key. also you can use forEach, and this is a link to Map description Commented Jan 17, 2020 at 9:02
  • @alex2007v If you need to iterate for map retrieval, you might as well just use an array. Commented Jan 17, 2020 at 9:12
  • @Amadan I know about this, that's why I give a link to Map documentation, because describe all available methods do not worth it. Commented Jan 17, 2020 at 9:16
  • @alex2007v: The link has comparison between Map and object, and says nothing about Array, though. My point was that retrieving from map by iteration will be O(N) just like looking through a list, but with much less overhead. Commented Jan 17, 2020 at 9:29

4 Answers 4

3

You can't. Map compares objects by object identity. [0, 1, 2, 3] !== [0, 1, 2, 3] as they are different objects, even if they hold the same data.

The nearest thing you can do is to try to convert the array to something you can compare meaningfully:

const map = new Map() const arr = [0,1,2,3] map.set(JSON.stringify([0, 1, 2, 3]), "I am some number") console.log(map.get(JSON.stringify([0, 1, 2, 3])))

Sign up to request clarification or add additional context in comments.

2 Comments

Changed the "different but identical" because it sounded like an oxymoron. Not sure if this is better - feel free to review.
@VLAZ You are right, I should probably have said "equal". "identical... but false when compared by identity" is contradictory. Then again, JavaScript does not have an equality operator capable of comparing arrays, so "equal" is hardly better. I like your wording.
1

That's correct, you have to use the same array (as in map.get(arr)), not just an equivalent array. Key comparison is like === (except that NaN matches itself). So just like this shows false:

console.log([0, 1, 2, 3] === [0, 1, 2, 3]); // false

...using map.get([0, 1, 2, 3]) is guaranteed not to find anything, because there isn't any entry in the map keyed by that array.

1 Comment

If you're interested, I cover Map (and Set, WeakMap, and WeakSet) in detail in Chapter 12 of my new book (see my profile for links).
0

Separate arrays aren't === to each other - your arr does not refer to the same array container as the [0,1,2,3] that you pass to map.get. To do something like this, you'd have to iterate over the map's keys and find the one whose values all match:

const map = new Map() const arr = [0,1,2,3]; map.set(arr, "I am some number") // Get a reference to the same `arr` whose key you set previously: const arrKey = [...map.keys()].find( key => Array.isArray(key) && JSON.stringify(key) === JSON.stringify([0, 1, 2, 3]) ); console.log(map.get(arrKey));

(but this is a pretty ugly thing to have to do - if you find yourself having to do this, usually it'd be better to use a different data structure)

Comments

0

You need the same object reference for getting the value from a Map.

If you like to use a starting part of the array as key, you need to get all keys from the map and check against with the new array.

var map = new Map, key0 = [0, 1, 2, 3], key1 = [0, 1, 2, 3]; map.set(key0, "I am some number"); console.log(map.get(key0)); // "I am some number" for (let key of map.keys()) if (key.join('|') === key1.join('|')) console.log(map.get(key));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.