jQuery .inArray() 总是正确的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4558086/
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-26 17:28:43  来源:igfitidea点击:

jQuery .inArray() always true?

jqueryarrays

提问by Charles Marsh

I'm trying to use inarray but its always returning true? Any ideas? (all li's are showing)

我正在尝试使用 inarray 但它总是返回 true?有任何想法吗?(所有的 li 都在展示)

$("#select-by-color-list li").hide();

// get the select
var $dd = $('#product-variants-option-0');

if ($dd.length > 0) { // make sure we found the select we were looking for

    // save the selected value
    var selectedVal = $dd.val();

    // get the options and loop through them
    var $options = $('option', $dd);
    var arrVals = [];
    $options.each(function(){
        // push each option value and text into an array
        arrVals.push({
            val: $(this).val(),
            text: $(this).text()
        });
    });




};

//This is where it is returning true...


if($.inArray('Aqua', arrVals)) {
    $("#select-by-color-list li#aqua").show();
    };
    if($.inArray('Army', arrVals)) {
    $("#select-by-color-list li#army").show();
    };

回答by user113716

You need to do this:

你需要这样做:

if( $.inArray('Aqua', arrVals) > -1 ) {

or this:

或这个:

if( $.inArray('Aqua', arrVals) !== -1 ) {

The $.inArray()methodreturns the 0based index of the item. If there's no item, it returns -1, which the if()statement will consider as true.

$.inArray()方法返回项目的0基础索引。如果没有项目,则返回-1if()语句将其视为true

From the docs:

从文档:

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

因为 JavaScript 将 0 视为大致等于 false(即 0 == false,但 0 !== false),如果我们检查数组中是否存在值,我们需要检查它是否不等于(或大于) ) -1。



EDIT:Instead of pushing both values into the array as an object, just use one or the other, so you have an Array of strings from which you can build a multiple selector.

编辑:不是将两个值作为对象推送到数组中,只需使用一个或另一个,这样您就有了一个字符串数组,您可以从中构建一个多重选择器。

One way is like this:

一种方法是这样的:

  // Create an Array from the "value" or "text" of the select options
var arrVals = $.map( $dd[0].options, function( opt, i ){
    return opt.value || opt.text;
});

  // Build a multiple selector by doing a join() on the Array.
$( "#" + arrVals.join(',#') ).show();

If the Array looks like:

如果数组看起来像:

['Army','Aqua','Bread'];

The resulting selector will look like:

结果选择器将如下所示:

$( "#Army,#Aqua,#Bread" ).show();

回答by Darren Sweeney

ES6 to the rescue! Although not jQuery I thought worth answering for future readers...

ES6 来救援!虽然不是 jQuery,但我认为值得为未来的读者回答......

ES6 introduces .includes()which works as you think $.inArraySHOULD work:

ES6 介绍了.includes()您认为$.inArray应该工作的工作方式:

const myArray = ["a", "b", "c"];

console.log(myArray.includes("a")) /* true */
console.log(myArray.includes("d")) /* false */

Array.prototype.includes()

Array.prototype.includes()