In javascript how can we identify whether an object is a Hash or an Array?

Javascript

Javascript Problem Overview


The output of my JSON call can either be an Array or a Hash. How do I distinguish between these two?

Javascript Solutions


Solution 1 - Javascript

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){
}

Solution 2 - Javascript

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
}

Solution 3 - Javascript

Did you mean "Object" instead of "Hash"?

>>> var a = [];
>>> var o = {};
>>> a instanceof Array
true
>>> o instanceof Array
false

Solution 4 - Javascript

I made a function for determining if it's a dictionary.

exports.is_dictionary = function (obj) {
	if(!obj) return false;
	if(Array.isArray(obj)) return false;
	if(obj.constructor != Object) return false;
	return true;
};

// return true
test.equal(nsa_utils.is_dictionary({}), true);
test.equal(nsa_utils.is_dictionary({abc:123, def:456}), true);

// returns false
test.equal(nsa_utils.is_dictionary([]), false);
test.equal(nsa_utils.is_dictionary([123, 456]), false);
test.equal(nsa_utils.is_dictionary(null), false);
test.equal(nsa_utils.is_dictionary(NaN), false);
test.equal(nsa_utils.is_dictionary('hello'), false);
test.equal(nsa_utils.is_dictionary(0), false);
test.equal(nsa_utils.is_dictionary(123), false);

Solution 5 - Javascript

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]);

Solution 6 - Javascript

For parsing json could come in handy :)

isArrayHashs = (attr) ->
  !!attr && attr.constructor == Array && isHash(attr[0])

isHash = (attr) ->
  !!attr && !$.isNumeric(attr) && attr.constructor == Object

attr[0].constructor must be:

  • String
  • Numeric
  • Array
  • Object
  • Undefined

Solution 7 - Javascript

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.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionakshatView Question on Stackoverflow
Solution 1 - JavascriptpawelView Answer on Stackoverflow
Solution 2 - JavascriptBorgarView Answer on Stackoverflow
Solution 3 - JavascriptJonny BuchananView Answer on Stackoverflow
Solution 4 - JavascriptneoneyeView Answer on Stackoverflow
Solution 5 - JavascriptSergey IlinskyView Answer on Stackoverflow
Solution 6 - JavascriptViktor IvliievView Answer on Stackoverflow
Solution 7 - JavascriptBob SteinView Answer on Stackoverflow