How can I determine that my data structure is of type Map()?
I've been looking everywhere, and can't find any method
I really want to get into using them because I can use objects as keys!
How can I determine that my data structure is of type Map()?
I've been looking everywhere, and can't find any method
I really want to get into using them because I can use objects as keys!
You can use the instanceof opeartor
var map = new Map();
var arr = [];
console.log(map instanceof Map);
console.log(map instanceof Array);
console.log(arr instanceof Map);
console.log(arr instanceof Array);
From the Documentation
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
You could use either instanceof
operator or the constructor
property and check against the object.
For more information read this: What's the difference between using instanceof and checking the constructor?
var map = new Map();
var arr = [];
console.log(map instanceof Map); // true
console.log(map instanceof Array); // false
console.log(map.constructor === Map); // true
console.log(map.constructor === Array); // false
console.log(arr instanceof Map); // false
console.log(arr instanceof Array); // true
console.log(arr.constructor === Map); // false
console.log(arr.constructor === Array); // true
// caveat!
console.log(arr.constructor === Object); // false
console.log(map.constructor === Object); // false
console.log(arr instanceof Object); // true
console.log(map instanceof Object); // true
You can even use Object.prototype.toString.call
, though instanceof
(@suren srapyan's answer) is more preferred
var a = [1,2,3];
var m = new Map()
a.forEach((x,i)=>m.set(i+1, x));
console.log(Object.prototype.toString.call(m))