在 Javascript 中检查对象是否为数组的最佳方法是什么?

发布于 2024-07-29 06:20:36 字数 199 浏览 4 评论 0原文

假设我有一个像这样的函数:

function foo(bar) {
    if (bar > 1) {
       return [1,2,3];
    } else {
       return 1;
    }
}

假设我调用 foo(1),我怎么知道它是否返回一个数组?

Say I have a function like so:

function foo(bar) {
    if (bar > 1) {
       return [1,2,3];
    } else {
       return 1;
    }
}

And say I call foo(1), how do I know it returns an array or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百善笑为先 2024-08-05 06:20:36

我使用这个函数:

function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}

jQuery.isArray 的实现方式。

查看这篇文章:

I use this function:

function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}

Is the way that jQuery.isArray is implemented.

Check this article:

姐不稀罕 2024-08-05 06:20:36
if(foo(1) instanceof Array)
    // You have an Array
else
    // You don't

更新:我必须回应下面的评论,因为人们仍然声称如果不亲自尝试这将行不通......

对于其他一些对象,这种技术不起作用(例如“ “instanceof String == false),但这适用于数组。 我在 IE6、IE8、FF、Chrome 和 Safari 中进行了测试。 在下面发表评论之前先尝试一下并亲自看看。

if(foo(1) instanceof Array)
    // You have an Array
else
    // You don't

Update: I have to respond to the comments made below, because people are still claiming that this won't work without trying it for themselves...

For some other objects this technique does not work (e.g. "" instanceof String == false), but this works for Array. I tested it in IE6, IE8, FF, Chrome and Safari. Try it and see for yourself before commenting below.

狼性发作 2024-08-05 06:20:36

从 ES5 开始,有 <代码>isArray

Array.isArray([])  // true

As of ES5 there is isArray.

Array.isArray([])  // true
胡渣熟男 2024-08-05 06:20:36

这是一种非常可靠的方法,取自 Javascript: the good parts,由 O'Reilly 发布:

if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
!(my_value.propertyIsEnumerable('length')) { // my_value is truly an array! }

我建议将其包装在您自己的函数中:

function isarray(my_value) {

    if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
        !(my_value.propertyIsEnumerable('length')) 
         { return true; }
    else { return false; }
}

Here is one very reliable way, take from Javascript: the good parts, published by O'Reilly:

if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
!(my_value.propertyIsEnumerable('length')) { // my_value is truly an array! }

I would suggest wrapping it in your own function:

function isarray(my_value) {

    if (my_value && typeof my_value === 'object' &&  typeof my_value.length === 'number' &&
        !(my_value.propertyIsEnumerable('length')) 
         { return true; }
    else { return false; }
}
甜警司 2024-08-05 06:20:36

为了使您的解决方案更加通用,您可能不关心它是否实际上是一个 Array 对象。 例如,document.getElementsByName() 返回一个“行为类似于”数组的对象。 如果对象具有“长度”属性,则可以假定“数组合规性”。

function is_array_compliant(obj){
    return obj && typeof obj.length != 'undefined';
}

To make your solution more general, you may not care whether it is actually an Array object. For example, document.getElementsByName() returns an object that "acts like" an array. "Array compliance" can be assumed if the object has a "length" property.

function is_array_compliant(obj){
    return obj && typeof obj.length != 'undefined';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文