How to check if an object is an array?

后端 未结 30 2881
名媛妹妹
名媛妹妹 2020-11-21 06:31

I\'m trying to write a function that either accepts a list of strings, or a single string. If it\'s a string, then I want to convert it to an array with just the one item so

相关标签:
30条回答
  • 2020-11-21 07:14

    As MDN says in here:

    use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays

    Like this:

    • Object.prototype.toString.call(arr) === '[object Array]', or

    • Array.isArray(arr)

    0 讨论(0)
  • 2020-11-21 07:14

    The following could be used if you know that your object doesn't have a concat method.

    var arr = [];
    if (typeof arr.concat === 'function') {
        console.log("It's an array");
    }

    0 讨论(0)
  • 2020-11-21 07:15

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray

    Array.isArray = Array.isArray || function (vArg) {
        return Object.prototype.toString.call(vArg) === "[object Array]";
    };
    
    0 讨论(0)
  • 2020-11-21 07:16

    This function will turn almost anything into an array:

    function arr(x) {
        if(x === null || x === undefined) {
            return [];
        }
        if(Array.isArray(x)) {
            return x;
        }
        if(isString(x) || isNumber(x)) {
            return [x];
        }
        if(x[Symbol.iterator] !== undefined || x.length !== undefined) {
            return Array.from(x);
        }
        return [x];
    }
    
    function isString(x) {
        return Object.prototype.toString.call(x) === "[object String]"
    }
    
    function isNumber(x) {
        return Object.prototype.toString.call(x) === "[object Number]"
    }
    

    It uses some newer browser features so you may want to polyfill this for maximum support.

    Examples:

    > arr(null);
    []
    > arr(undefined)
    []
    > arr(3.14)
    [ 3.14 ]
    > arr(1/0)
    [ Infinity ]
    > gen = function*() { yield 1; yield 2; yield 3; }
    [Function: gen]
    > arr(gen())
    [ 1, 2, 3 ]
    > arr([4,5,6])
    [ 4, 5, 6 ]
    > arr("foo")
    [ 'foo' ]
    

    N.B. strings will be converted into an array with a single element instead of an array of chars. Delete the isString check if you would prefer it the other way around.

    I've used Array.isArray here because it's the most robust and also simplest.

    0 讨论(0)
  • 2020-11-21 07:17

    I know, that people are looking for some kind of raw javascript approach. But if you want think less about, take a look here: http://underscorejs.org/#isArray

    _.isArray(object) 
    

    Returns true if object is an Array.

    (function(){ return _.isArray(arguments); })();
    => false
    _.isArray([1,2,3]);
    => true
    
    0 讨论(0)
  • 2020-11-21 07:17
    function isArray(value) {
        if (value) {
            if (typeof value === 'object') {
                return (Object.prototype.toString.call(value) == '[object Array]')
            }
        }
        return false;
    }
    
    var ar = ["ff","tt"]
    alert(isArray(ar))
    
    0 讨论(0)
提交回复
热议问题