0

I'm writing a some NodeJS code that is being passed a Map Iterator. What is the correct way to examine a Javascript object to determine that it is a "Map Iterator"?

I've tried:

typeof myMap.keys() returns 'Object'
typeof myMap.keys().next === 'function' returns true

So I'm checking to see if the object being passed has a next() function. Is there a better way to do this?

The Chrome debugger the object is a Map Iterator, so I'm guessing there is a more correct Javascript way to do this.

PatS
  • 8,833
  • 12
  • 57
  • 100
  • Does this feet what you want? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries – ChunTingLin Nov 23 '17 at 01:32
  • what does `Object.prototype.toString.call(myMap.keys())` output? – Jaromanda X Nov 23 '17 at 01:34
  • 1
    `instanceof` can do the work. Just check if `myMap instanceof Map` . Incase this is true, `myMap.keys()` will be a MapIterator – Prasanna Nov 23 '17 at 05:30
  • @Prasanna, I like this as the answer. Do you want to answer this and I'll select your response as the answer? – PatS Nov 27 '17 at 23:36
  • Why exactly do you care about this? Every iterator that yields key-value-tuples should work for your function! – Bergi Nov 27 '17 at 23:39
  • @Bergi, I was writing a utility function Util.myTypeof(obj) where obj could be anything in Javascript. I found the Map Iterator wasn't being handled correctly by myTypeof() so I asked this question so I could fix it. – PatS Nov 28 '17 at 00:24
  • @PatS How was it handled, could you post your code? How did you expect it to be handled (and why)? Notice that JS uses mostly duck typing, so trying to distinguish objects of different kinds is usually futile. – Bergi Nov 28 '17 at 01:13
  • @Bergi, The code was basically `mysort(obj, comparator){ if (Util.myTypeof(obj) === 'map-iterator') { return Array.from(obj).sort(comparator); } . . . `. I expect to return a sorted array and be able to handle Array, Object returns property names of direct object, Map returns keys in map, Set returns keys in set. I asked this because when I passed a Map Iterator the code wasn't handling it correctly. I think I understand your point about Javascript typing. If there is a better solution please post one. Thanks. – PatS Nov 30 '17 at 03:12
  • @PatS Sounds like you really wanted `(Array.isArray(obj) ? obj : obj instanceof Set ? Array.from(obj) : obj instanceof Map ? Array.from(obj.keys()) : Object.keys(obj)).sort(comparator)`. Yes, you should not pass a map entries iterator if you expect the keys - either pass the map itself and distinguish it [as that](https://stackoverflow.com/a/29926193/1048572) or pass the `map.keys()` iterator. – Bergi Nov 30 '17 at 03:30
  • @Bergi, lol. That's it in one line. :-) Thanks for your interest in helping me out. – PatS Nov 30 '17 at 03:34

2 Answers2

1

If all you want to do is check if it's a Map (which can be iterated over), you can do that with myMap instanceof Map, which returns a boolean.

jhpratt
  • 6,841
  • 16
  • 40
  • 50
0

You you get the Symbol.toStringTag property of the object

var m = new Map;
m.set(1, [123]);
var gen = m.entries();

console.log(gen.toString()); // "[object Map Iterator]";
console.log(gen[Symbol.toStringTag] === "Map Iterator"); // true
guest271314
  • 1
  • 15
  • 104
  • 177