在 javascript 中我们如何识别一个对象是一个散列还是一个数组?

我的 JSON 调用的输出可以是 Array 或 Hash。我如何区分这两者?

42532 次浏览

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
}

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

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


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

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

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

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

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]' && ! (Object(z) instanceof String);
}


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

The && clause above is a brutish fix for this obscure white-box test:

console.assert(false === is_associative_array("[object Object]"));

Caution: these functions will not be efficient for large or many objects.