Check that value is object literal?

后端 未结 12 670
逝去的感伤
逝去的感伤 2020-12-01 10:57

I have a value and want to know if it\'s an iteratable object literal, before I iterate it.

How do I do that?

12条回答
  •  离开以前
    2020-12-01 11:21

    Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following

    testVar instanceof Object && !Array.isArray(testVar) && testVar !== null
    

提交回复
热议问题