如何检查变量是否是JavaScript中的数组? [重复]

本文翻译自:How do you check if a variable is an array in JavaScript? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I would like to check whether a variable is either an array or a single value in JavaScript. 我想检查变量是JavaScript中的数组还是单个值。

I have found a possible solution... 我找到了一个可能的解决方案......

if (variable.constructor == Array)...

Is this the best way this can be done? 这是最好的办法吗?


#1楼

参考:https://stackoom.com/question/3Deo/如何检查变量是否是JavaScript中的数组-重复


#2楼

I was using this line of code: 我正在使用这行代码:

if (variable.push) {
   // variable is array, since AMAIK only arrays have push() method.
}

#3楼

I liked the Brian answer: 我喜欢布莱恩的回答:

function is_array(o){
    // make sure an array has a class attribute of [object Array]
    var check_class = Object.prototype.toString.call([]);
    if(check_class === '[object Array]')    {
        // test passed, now check
        return Object.prototype.toString.call(o) === '[object Array]';
    } else{
        // may want to change return value to something more desirable
        return -1; 
    }
}

but you could just do like this: 但你可以这样做:

return Object.prototype.toString.call(o) === Object.prototype.toString.call([]);

#4楼

If you're only dealing with EcmaScript 5 and above then you can use the built in Array.isArray function 如果您只处理EcmaScript 5及更高版本,则可以使用内置的Array.isArray函数

eg, 例如,

Array.isArray([])    // true
Array.isArray("foo") // false
Array.isArray({})    // false

#5楼

For those who code-golf, an unreliable test with fewest characters: 对于那些编码高尔夫的人来说,一个不可靠的测试,其中人物最少:

function isArray(a) {
  return a.map;
}

This is commonly used when traversing/flattening a hierarchy: 这在遍历/展平层次结构时通常使用:

function golf(a) {
  return a.map?[].concat.apply([],a.map(golf)):a;
}

input: [1,2,[3,4,[5],6],[7,[8,[9]]]]
output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

#6楼

In Crockford's JavaScript The Good Parts , there is a function to check if the given argument is an array: 在Crockford的JavaScript The Good Parts中 ,有一个函数来检查给定的参数是否是一个数组:

var is_array = function (value) {
    return value &&
        typeof value === 'object' &&
        typeof value.length === 'number' &&
        typeof value.splice === 'function' &&
        !(value.propertyIsEnumerable('length'));
};

He explains: 他解释说:

First, we ask if the value is truthy. 首先,我们询问价值是否真实。 We do this to reject null and other falsy values. 我们这样做是为了拒绝null和其他有价值的值。 Second, we ask if the typeof value is 'object'. 其次,我们询问typeof值是否为'object'。 This will be true for objects, arrays, and (weirdly) null. 这对于对象,数组和(怪异地)null都是如此。 Third, we ask if the value has a length property that is a number. 第三,我们询问该值是否具有数字的长度属性。 This will always be true for arrays, but usually not for objects. 这对于数组总是如此,但通常不适用于对象。 Fourth, we ask if the value contains a splice method. 第四,我们询问该值是否包含拼接方法。 This again will be true for all arrays. 对于所有阵列,这也是如此。 Finally, we ask if the length property is enumerable (will length be produced by a for in loop?). 最后,我们询问长度属性是否可枚举(长度是否由for in循环产生?)。 That will be false for all arrays. 对于所有阵列来说都是错误的。 This is the most reliable test for arrayness that I have found. 这是我发现的最可靠的数组测试。 It is unfortunate that it is so complicated. 不幸的是它太复杂了。

发布了0 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105197291