javascript check if dictionary

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

    Would that work ?

    function isDictObj(obj: any) {
      try {
        const test = {...obj}
      } catch(err) {
        return false
      }
      return true
    }
    

    My logic is that if it is a dictionnary object (key:values), then using spread operator with {} should work, otherwise throw an exception

提交回复
热议问题