How to judge whether an object is an array?

Method one: instanceof
instanceof is used to determine whether a variable is an instance of an object

var arr=[];
console.log(arr instanceof Array) 

//返回true

Method 2: The constructor
constructor property returns a reference to the array function that created this object, that is, the constructor corresponding to the returned object

console.log([].constructor == Array)

Method 3: Array.isArray () is
used to determine whether the passed value is an Array. The value that
obj
needs to be checked, true if the object is Array; otherwise, false.

Array.isArray(obj)

 

 

Published 203 original articles · praised 8 · 10,000+ views

Guess you like

Origin blog.csdn.net/z591102/article/details/105575464