javascript check if dictionary

后端 未结 7 2381
旧巷少年郎
旧巷少年郎 2021-02-07 11:37

I have a simple program like:

var a = {\'a\': 1, \'b\': 2}
console.log(a)
console.log(a instanceof Array)
console.log(a.constructor instanceof Array)
         


        
7条回答
  •  暖寄归人
    2021-02-07 12:04

    I use the toString method in Object.prototype, This works like a charm in all the cases(Array,null,undefined etc).

    var array_var=[1,2];
    var dict_var={
    'a':'hey',
    'b':'hello'
    };
    console.log(Object.prototype.toString.call(dict_var) === '[object Object]');//returns true
    console.log(Object.prototype.toString.call(array_var) === '[object Object]');//returns false  
    

    In short the toString method is used to represent the object, for example the toString method for an array returns '[object Array]'

提交回复
热议问题