The output of my JSON call can either be an Array or a Hash. How do I distinguish between these two?
The output of my JSON call can either be an Array or a Hash. How do I distinguish between these two?
Modern browsers support the Array.isArray(obj) method.
See MDN for documentation and a polyfill.
= original answer from 2008 =
you can use the constuctor property of your output:
if(output.constructor == Array){
}
else if(output.constructor == Object){
}
Is object:
function isObject ( obj ) {
return obj && (typeof obj === "object");
}
Is array:
function isArray ( obj ) {
return isObject(obj) && (obj instanceof Array);
}
Because arrays are objects you'll want to test if a variable is an array first, and then if it is an object:
if (isArray(myObject)) {
// do stuff for arrays
}
else if (isObject(myObject)) {
// do stuff for objects
}
Did you mean "Object" instead of "Hash"?
>>> var a = [];
>>> var o = {};
>>> a instanceof Array
true
>>> o instanceof Array
false
Check for "constructor" property on the object. It is Array - it is an array object.
var a = {
'b':{length:0},
'c':[1,2]
}
if (a.c.constructor == Array)
for (var i = 0; i < a.c.length; i++)
alert(a.c[i]);
else
for (var s in a.b);
alert(a.b[s]);
A more practical and precise term than object or hash or dictionary may be associative array. Object could apply to many undesirables, e.g. typeof null === 'object' or [1,2,3] instanceof Object. The following two functions work since ES3 and are mutually exclusive.
function is_array(z) {
return Object(z) instanceof Array;
}
console.assert(true === is_array([]));
console.assert(true === is_array([1,2,3]));
console.assert(true === is_array(new Array));
console.assert(true === is_array(Array(1,2,3)));
console.assert(false === is_array({a:1, b:2}));
console.assert(false === is_array(42));
console.assert(false === is_array("etc"));
console.assert(false === is_array(null));
console.assert(false === is_array(undefined));
console.assert(false === is_array(true));
console.assert(false === is_array(function () {}));
function is_associative_array(z) {
return String(z) === '[object Object]';
}
console.assert(true === is_associative_array({a:1, b:2}));
console.assert(true === is_associative_array(new function Legacy_Class(){}));
console.assert(true === is_associative_array(new class ES2015_Class{}));
console.assert(false === is_associative_array(window));
console.assert(false === is_associative_array(new Date()));
console.assert(false === is_associative_array([]));
console.assert(false === is_associative_array([1,2,3]));
console.assert(false === is_associative_array(Array(1,2,3)));
console.assert(false === is_associative_array(42));
console.assert(false === is_associative_array("etc"));
console.assert(false === is_associative_array(null));
console.assert(false === is_associative_array(undefined));
console.assert(false === is_associative_array(true));
console.assert(false === is_associative_array(function () {}));
Notice how this will treat the instance of a class as an associative array. (But not the instance of a built-in class, such as Date.)