In javascript how can we identify whether an object is a Hash or an Array?

后端 未结 7 1966
忘了有多久
忘了有多久 2020-12-02 18:16

The output of my JSON call can either be an Array or a Hash. How do I distinguish between these two?

相关标签:
7条回答
  • 2020-12-02 18:52

    For parsing json could come in handy :)

    isArrayHashs = (attr) ->
      !!attr && attr.constructor == Array && isHash(attr[0])
    
    isHash = (attr) ->
      !!attr && !$.isNumeric(attr) && attr.constructor == Object
    

    attr[0].constructor must be:

    • String
    • Numeric
    • Array
    • Object
    • Undefined
    0 讨论(0)
提交回复
热议问题