我需要确定数组中是否存在一个值。

我正在使用以下函数:

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

上面的函数总是返回false。

数组值和函数调用如下所示:

arrValues = ["Sam","Great", "Sample", "High"]
alert(arrValues.contains("Sam"));

当前回答

var contains = function(needle) {
    // Per spec, the way to identify NaN is that it is not equal to itself
    var findNaN = needle !== needle;
    var indexOf;

    if(!findNaN && typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                var item = this[i];

                if((findNaN && item !== item) || item === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle) > -1;
};

你可以这样使用它:

var myArray = [0,1,2],
    needle = 1,
    index = contains.call(myArray, needle); // true

CodePen验证/使用

其他回答

使用像lodash这样的库几乎总是更安全,因为存在跨浏览器兼容性和效率方面的所有问题。

效率,因为你可以保证在任何给定的时间,一个非常流行的库,如下划线,将有最有效的方法来完成这样的效用函数。

_.includes([1, 2, 3], 3); // returns true

如果你担心通过包含整个库而添加到应用程序中的大量内容,那么你可以单独包含功能:

var includes = require('lodash/collections/includes');

注意:在旧版本的lodash中,这是_.contains()而不是_.includes()。

如果您可以访问ECMA 5,您可以使用some方法。

MDN SOME方法链接

arrValues = ["Sam","Great", "Sample", "High"];

function namePresent(name){
  return name === this.toString();
}
// Note:
// namePresent requires .toString() method to coerce primitive value
// i.e. String {0: "S", 1: "a", 2: "m", length: 3, [[PrimitiveValue]]: "Sam"}
// into
// "Sam"

arrValues.some(namePresent, 'Sam');
=> true;

如果您可以访问ECMA 6,则可以使用includes方法。

MDN包含方法链接

arrValues = ["Sam","Great", "Sample", "High"];

arrValues.includes('Sam');
=> true;
var contains = function(needle) {
    // Per spec, the way to identify NaN is that it is not equal to itself
    var findNaN = needle !== needle;
    var indexOf;

    if(!findNaN && typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                var item = this[i];

                if((findNaN && item !== item) || item === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle) > -1;
};

你可以这样使用它:

var myArray = [0,1,2],
    needle = 1,
    index = contains.call(myArray, needle); // true

CodePen验证/使用

另一种选择是使用Array。部分(如有)采用以下方式:

Array.prototype.contains = function(obj) {
  return this.some( function(e){ return e === obj } );
}

传递给Array的匿名函数。当且仅当数组中存在与obj相同的元素时,一些函数将返回true。如果没有这样的元素,则该函数对于数组的任何元素都不会返回true,因此array .数组将返回true。有些也会返回false。

这通常是indexOf()方法的用途。你会说:

return arrValues.indexOf('Sam') > -1