javascript check if dictionary

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

    You can use this to check either your dict(data) is a dictionary or not !

    var dict = { a: 1, b: { c: 3, d: 4 }, e: 9 };
    
    // this function will true / false
    
    const isDict = dict => {
      return typeof dict === "object" && !Array.isArray(dict);
    };
    
    console.log(isDict(dict));  // true
    

    this will return you true if it is a dictionary otherwise return false

提交回复
热议问题