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

后端 未结 7 1965
忘了有多久
忘了有多久 2020-12-02 18:16

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

相关标签:
7条回答
  • 2020-12-02 18:29

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

    >>> var a = [];
    >>> var o = {};
    >>> a instanceof Array
    true
    >>> o instanceof Array
    false
    
    0 讨论(0)
  • 2020-12-02 18:31

    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);
    
    0 讨论(0)
  • 2020-12-02 18:33

    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){
    }
    
    0 讨论(0)
  • 2020-12-02 18:35

    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]);
    
    
    0 讨论(0)
  • 2020-12-02 18:39

    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
    }
    
    0 讨论(0)
  • 2020-12-02 18:41

    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 naturally exclusive.

    function is_array(z) {
        return Object.prototype.toString.call(z) === '[object 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 Object.prototype.toString.call(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.)

    Thanks to RobG's solution along these lines for an Array.isArray() polyfill. This taps the Object class's native toString() method, which tersely and efficiently reports type.

    0 讨论(0)
提交回复
热议问题