最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to test that a variable is type Map or Array? - Stack Overflow

programmeradmin3浏览0评论

In ES6, how can I test if a variable is an Array or a Map?

instance.constructor.name === 'Map' is a risky habit, even if it's core type, doing this with your own class when minified will break the test.

What is the most reliable way to verify that the variable is an instance of Map

In ES6, how can I test if a variable is an Array or a Map?

instance.constructor.name === 'Map' is a risky habit, even if it's core type, doing this with your own class when minified will break the test.

What is the most reliable way to verify that the variable is an instance of Map

Share Improve this question edited Feb 23, 2019 at 11:41 Dimitri Kopriwa asked Feb 23, 2019 at 11:31 Dimitri KopriwaDimitri Kopriwa 14.4k32 gold badges112 silver badges230 bronze badges 2
  • 3 An array is never a Map, they're entirely different constructs. – CertainPerformance Commented Feb 23, 2019 at 11:32
  • I noticed that you edited your question in a way that invalidates existing answers. Doing so is bad form, because it confuses readers, and makes the people who wrote those answers look like idiots, because their answer no longer matches the question. Please consider rolling back your edit to the previous version. – Jörg W Mittag Commented Apr 24, 2021 at 17:49
Add a comment  | 

6 Answers 6

Reset to default 6

const getType = obj => Object.prototype.toString.call(obj).slice(8, -1);

const isArray = obj => getType(obj) === 'Array';

const isMap = obj => getType(obj) === 'Map';


const arr = [];

console.log(isArray(arr)); // true


const map = new Map();

console.log(isMap(map)); // true

You can use instanceof. It will return true/false if the object is a map or not

var a = new Map;
console.log(a instanceof Map);

For checking array use isArray method

var a= new Array;
console.log(Array.isArray(a))

Instead of checking the constructor's .name (string) property, just check whether the constructor itself is === Map:

const m = new Map();
console.log(m.constructor === Map);

You could check with Array.isArray, because Map is no Array.

For checking the instance, you could take the instanceof operator.

var array = [],
    map = new Map;
    
console.log(Array.isArray(array)); //  true
console.log(Array.isArray(map));   // false
console.log(array instanceof Map); // false
console.log(map instanceof Map);   //  true

You don't need to test if an Array is a Map in JavaScript, since an Array is an Array, it can never be a Map.

In fact, they are not even the same kind of collection, an Array is an indexed collection whereas a Map is a keyed collection.

An Array can naver be a Map in javascript. You may however be confused with Object or map. To check if the javascript variable is Map you can make use of instanceof

instance instanceof Map

To test is the variable is an instance of an Array, you can write Array.isArray(instance)

var instance = new Map;
console.log(instance instanceof Map); // true
console.log(instance instanceof Array); //false

发布评论

评论列表(0)

  1. 暂无评论