在 Javascript 数组中查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6638383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:35:08  来源:igfitidea点击:

Find element in Javascript Array

javascriptarrays

提问by djreed

Possible Duplicate:
Javascript - array.contains(obj)

可能重复:
Javascript - array.contains(obj)

What's wrong with this:

这有什么问题:

var zipCodes =(['90001','90002','90003']);

Test if the value exists in the array zipCodes

测试数组中是否存在该值 zipCodes

if('90001' in zipCodes) {
  alert('True');
};

回答by user113716

The inoperator looks at property names, not values.

in操作者查看属性的名称,而不是价值。

Because it's an Array, the property names will be the indices of the Array.

因为它是一个数组,属性名称将是数组的索引。

If you're only supporting a modern environment, you could use Array.prototype.indexOf().

如果您只支持现代环境,则可以使用Array.prototype.indexOf().

if(zipCodes.indexOf('90001') > -1) {

If you need to support environments that don't have .indexOf(), you could implement the MDN fix.

如果您需要支持没有 的环境.indexOf(),您可以实施MDN 修复

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";

        if (this === void 0 || this === null) throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) return -1;

        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) // shortcut for verifying if it's NaN
            n = 0;
            else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) n = (n > 0 || -1) * Math.floor(Math.abs(n));
        }

        if (n >= len) return -1;

        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);

        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) return k;
        }
        return -1;
    };
}

回答by Ben Simpson

If you want to check if the array contains a given value, you can use the indexOf method to check for the position of an item. If the item is not found in the array, a -1 is returned:

如果要检查数组是否包含给定值,可以使用 indexOf 方法检查项目的位置。如果在数组中找不到该项目,则返回 -1:

var zipCodes =(['90001','90002','90003']);
zipCodes.indexOf('90001') // 0
zipCodes.indexOf('90002') // 1
zipCodes.indexOf('90003') // 2
zipCodes.indexOf('90004') // -1

if(zipCodes.indexOf('90001') != -1) {
  alert('True');
};

See more at http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm

http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm查看更多

回答by jfriend00

Use an object instead. If this is all you're trying to do with the array, then an object is a much more efficient way to do a lookup list.

改用一个对象。如果这就是您试图对数组执行的全部操作,那么对象是一种更有效的查找列表方式。

var zipCodes = {"90001": true, "90002": true, "90003": true};

if ('90001' in zipCodes) {
    alert('True');
}

jsfiddle here to see it work: http://jsfiddle.net/jfriend00/ZNGTq/

jsfiddle在这里看到它的工作:http: //jsfiddle.net/jfriend00/ZNGTq/

回答by PaulM

You need something like this:

你需要这样的东西:

var zipCodes =(['90001','90002','90003']);

if (zipCodes.has('90001')) {
  ....
}

    Array.prototype.has=function(v){
        for (i=0;i<this.length;i++){
           if (this[i]==v) return i;
        }
        return false;
    }

See this for more info:

请参阅此了解更多信息:

http://snook.ca/archives/javascript/testing_for_a_v

http://snook.ca/archives/javascript/testing_for_a_v

....

....

回答by Kon

Because inchecks for a property of an object. Check this out for converting "in object" to "in array": Testing for a Value in JavaScript Array

因为in检查对象的属性。看看这个将“in object”转换为“in array”: Testing for a Value in JavaScript Array