How do I check if an array includes an object in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains an object?

This is the only way I know to do it:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

Is there a better and more concise way to accomplish this?

This is very closely related to Stack Overflow question Best way to find an item in a JavaScript Array? which addresses finding objects in an array using indexOf .


Current browsers have Array#includes , which does exactly that, is widely supported, and has a polyfill for older browsers.

You can also use Array#indexOf , which is less direct, but doesn't require Polyfills for out of date browsers.

jQuery offers $.inArray , which is functionally equivalent to Array#indexOf .

underscore.js, a JavaScript utility library, offers _.contains(list, value) , alias _.include(list, value) , both of which use indexOf internally if passed a JavaScript array.

Some other frameworks offer similar methods:

  • Dojo Toolkit: dojo.indexOf(array, value, [fromIndex, findLast])
  • Prototype: array.indexOf(value)
  • MooTools: array.indexOf(value)
  • MochiKit: findValue(array, value)
  • MS Ajax: array.indexOf(value)
  • Ext: Ext.Array.contains(array, value)
  • Lodash: _.includes(array, value, [from]) (is _.contains prior 4.0.0)
  • ECMAScript 2016: array.includes(value)
  • Notice that some frameworks implement this as a function, while others add the function to the array prototype.


    Update: As @orip mentions in comments, the linked benchmark was done in 2008, so results may not be relevant for modern browsers. However, you probably need this to support non-modern browsers anyway and they probably haven't been updated since. Always test for yourself.

    As others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. So you may want to rewrite your code as follows:

    function contains(a, obj) {
        var i = a.length;
        while (i--) {
           if (a[i] === obj) {
               return true;
           }
        }
        return false;
    }
    

    Of course, you may as well extend Array prototype:

    Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
                return true;
            }
        }
        return false;
    }
    

    And now you can simply use the following:

    alert([1, 2, 3].contains(2)); // => true
    alert([1, 2, 3].contains('2')); // => false
    

    indexOf maybe, but it's a "JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard."

    Example:

    [1, 2, 3].indexOf(1) => 0
    ["foo", "bar", "baz"].indexOf("bar") => 1
    [1, 2, 3].indexOf(4) => -1
    

    AFAICS Microsoft does not offer some kind of alternative to this, but you can add similar functionality to arrays in Internet Explorer (and other browsers that don't support indexOf ) if you want to, as a quick Google search reveals (for example, this one).

    链接地址: http://www.djcxy.com/p/218.html

    上一篇: 不区分大小写'包含(字符串)'

    下一篇: 如何检查数组是否包含JavaScript中的对象?