Differentiating between arrays and “hashes” in Javascript

后端 未结 5 1664
再見小時候
再見小時候 2021-02-07 13:29

In order to make the syntax for one of my functions nicer, I need to be able to tell whether a specific parameter is an array or \"hash\" (which I know are just objects).

<
5条回答
  •  抹茶落季
    2021-02-07 14:06

    Check for the length property:

    "length" in {foo:"bar"}   //false
    "length" in ["foo","bar"] //true
    

提交回复
热议问题