For the purpose of creating a condition while recursively iterating over a JSON object, I'm trying to find a clean way to differentiate different object types.
Consider this:
var aJSON = {"foo" : 1, "bar" : 2};
var anArray = [1,2,3,4,5];
var aDate = new Date('2013-03-10T02:00:00Z');
var anISODate = user.timesTheyLoggedIn[0] // comes from MongoDB
console.log(typeof aJSON); // object
console.log(typeof anArray); // object
console.log(typeof aDate); // object
console.log(typeof anISODate); // object
All types of objects are 'objects' with no more info.
For now, my use case only requires detecting an ISODate object, so I'm taking advantage of ISODate objects having a resetTime function to differentiate them from others, like so:
if (typeof(val) === "object" && val.resetTime === undefined) { //do stuff...
But there must be a cleaner, better way to detect what kind of object an object is, a val.isArray() kinda thing...
How would do it?
PS: "different object types" is in quotes because there really is only one object type in JS, right?
aJSONis an object not a JSON formatted string