I have a value and want to know if it's an iteratable object literal, before I iterate it.
How do I do that?
See Question&Answers more detail:osI have a value and want to know if it's an iteratable object literal, before I iterate it.
How do I do that?
See Question&Answers more detail:osThis should do it for you:
if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
// do your iteration
}
From ECMAScript 5 Section 8.6.2 if you're interested:
The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).