0
$.post('service.php?getPhotos', function(data){ 

    var photoIds = [];

    $.each(data, function(){
        photoIds.push(this.id);
    });

    console.log(_photoId); //7962669392
    console.log(photoIds); //["7980686507", "7962669392", "7962163506"]
    console.log($.inArray(_photoId, photoIds)); //-1

});

Why doesnt console.log($.inArray(_photoId, photoIds)); return 1?

3 Answers 3

3

string vs integer I would imagine. Different types would mean that inArray does not see them as the same. Make sure you are using a string instead of an integer and this should work.

Sign up to request clarification or add additional context in comments.

Comments

3

It seems like _photoId is a number but photoIds contains strings. Try this:

$.inArray(''+ _photoId, photoIds)

Comments

1

This is because the $.inArray function also takes the type into account. It will not say that an integer is the same as a string. This is also explained in the first comment in the documentation.

You can use _photoId.toString() to get the string representation of the value.

Also note that ECMAScript (Javascript) have a native function called indexOf which does exactly the same thing as the jQuery one:

photoIds.indexOf(_photoId.toString())

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.