1

so I have an Array and I want to loop through the Array and check if the values in it match some strings and if so do something.

This is what I have so far:

var rgt_count = a_related_genres_leon.length; // <- 3 items

for (var i = 0; i < rgt_count; i++) {

    if inArray("Music", a_related_genres_leon) {
        console.log('Music');
    } else if inArray("TV", a_related_genres_leon) {
        console.log('TV');
    } else if inArray("Comedy", a_related_genres_leon) {
        console.log('Comedy');
    }
}

I'm getting a Uncaught SyntaxError: Unexpected token ( error right now.

I also tried this

if text($.inArray("Music", a_related_genres_leon)) {
        console.log('Music');
    } else if text($.inArray("TV", a_related_genres_leon)) {
        console.log('TV');
    } else if text($.inArray("Comedy", a_related_genres_leon)) {
        console.log('Comedy');
    }
1
  • 2
    Syntax error because syntax of if clause is different in JavaScript.
    – VisioN
    Apr 26, 2013 at 17:24

3 Answers 3

6

Either search for a particular element in the array:

if ($.inArray("Music", a_related_genres_leon) > -1) {
    console.log('Music');
}
if ($.inArray("TV", a_related_genres_leon) > -1) {
    console.log('TV');
}
if ($.inArray("Comedy", a_related_genres_leon) > -1) {
    console.log('Comedy');
}

or iterate over all elements in the array

var rgt_count = a_related_genres_leon.length; // <- 3 items
for (var i = 0; i < rgt_count; i++) {
    console.log(a_related_genres_leon[i]);
}

but do not do a mixture of both.

2
  • Thanks! So fast, 8 more mins before I can check this, I also just found out that 'if (jQuery.inArray("Music", a_related_genres_leon)) {' works too :)
    – Leon Gaban
    Apr 26, 2013 at 17:31
  • @Leon: inArray returns the index of the element though, so if the element is at index 0, the condition will evaluate to false, which is wrong. You have to explicitly check that the return value is not -1 (which means the element is not contained in the array). Apr 26, 2013 at 17:32
2

Refer this for correct syntax and usage

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/if...else http://api.jquery.com/jQuery.inArray/

if($.inArray("Music", a_related_genres_leon) > -1) {
    console.log('Music');
} else if($.inArray("TV", a_related_genres_leon) > -1) {
    console.log('TV');
} else if($.inArray("Comedy", a_related_genres_leon) > -1) {
    console.log('Comedy');
}
1

Try this:

if ($.inArray("Music", a_related_genres_leon) > -1) {
    console.log('Music');
} else if ($.inArray("TV", a_related_genres_leon) > -1) {
    console.log('TV');
} else if ($.inArray("Comedy", a_related_genres_leon) > -1) {
    console.log('Comedy');
}

From the jQuery.inArray() API Docs

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

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.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.