javascript check if dictionary

后端 未结 7 2380
旧巷少年郎
旧巷少年郎 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:14

    The simplest approach to check if something is a dictionary in Javascript in a way that will not also return true when given an array is:

    if (a.constructor == Object) {
        // code here...
    }
    

    This was inspired by the answer here.

提交回复
热议问题