kiko carisse
kiko carisse

Reputation: 1784

pull an item out of an object array in jQuery

I am having a really hard time understanding this. I am selecting all image tags based on whether they have the word /resize/ in the src of the img tag (I think its returning an array...?). Then I want to check each src to see if there is a file where specified in the src location of that img tag. If the image does exist, then I want to pull out that image from the array, but it won't remove. It's also saying Uncaught TypeError: images.indexOf is not a function.

Here is my code -

var images = [];
var images = $(' img[src*="/resize/"]');

console.log(images);

$.each(images, function(key, value){
    var getFile = $(value).data('src');
    $.ajax({
        type: 'HEAD',
        url: getFile,
        success: function() {
            var index = images.indexOf(key);

            if (index > -1) {
                images.splice(index, 1);
            }
        },
        error: function() {

        }
    });
    x++;
});

Can someone please help? This is killing me...

Upvotes: 1

Views: 311

Answers (1)

Phillip
Phillip

Reputation: 6253

The $ (or jQuery) constructor returns a jQuery object. jQuery objects behave much like an array with a length, slice method etc. as well as elements indexed 0, 1, 2… etc.

But a jQuery object is not a true javascript Array and does not have many of the Array methods like join, reverse or in your case indexOf.

Instead you can use the index method. Read more about it on jQuery API Documentation

Upvotes: 1

Related Questions