20

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

$("#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();
    };

2 Answers 2

58

You need to do this:

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

or this:

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

The $.inArray() method returns the 0 based index of the item. If there's no item, it returns -1, which the if() statement will consider as 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.


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();
Sign up to request clarification or add additional context in comments.

8 Comments

here is the select list...<select class="single-option-selector" id="product-variants-option-0"><option value="Gunmetal Heather">Gunmetal Heather</option><option value="Kelly Green">Kelly Green</option><option value="Blackberry">Blackberry</option></select>
@Charles: This is because the arrVals Array doesn't contain 'Army' or 'Aqua', but instead contains an object where either the key or value (not sure which) has the String you want.
How can I base an if statement on it?
@Charles: Is there some reason you need both the val() and text() as the key/value pair? Why not just one or the other? If you do that, you could actually skip the inArray, and just build a multiple selector that selects all elements in the Array. I'll give an example in a minute.
Thanks VERY much! I've just realised I was trying something out with that and it shouldn't be in there any longer I only need the val()... Thankg again!
|
2

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

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

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

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

Array.prototype.includes()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.