imagesloaded icon indicating copy to clipboard operation
imagesloaded copied to clipboard

obj is not iterable

Open Craigy- opened this issue 2 years ago • 2 comments

After updating this script from 4.1.0 to 5.0.0 without any changes of initialization code, it's now thrown error and doesn't work:

Uncaught TypeError: obj is not iterable

Initialization code:

$('.js-b').each(function () {
    console.log($(this));
    $(this).imagesLoaded().done(function () {
      ...
    });
});

console.log => Object { 0: div.all-b.vertical-b.js-b, context: div.all-b.vertical-b.js-b, length: 1 }

Craigy- avatar Oct 19 '22 21:10 Craigy-

@Craigy- I was having the same issue.

I made a simple test with the jquery version of images loaded with a simple container loading a number of images as immediate children and was still getting the 'obj not iterable' error.

I just went in and updated the code in the in the script to use the Object.values() method to convert to an array so that I didn't run into the iterable issue with the Object.

I updated this if (isArrayLike) return [...obj]; To this if (isArrayLike) return Object.values(obj);

And all is working now,

dan-bizango avatar Dec 08 '22 15:12 dan-bizango

The issue also occurs on WordPress 6.4.2.

The following combination is running there:

  • imagesloaded 5.0.0
  • masonry 4.2.2

After modifying line 138 in the imagesloaded script, the script runs without errors:

Line 138:

if ( isArrayLike ) return [ ...obj ];

Changed to:

if (isArrayLike) return Object.values(obj);

Please take a close look at the issue and fix it. All WP pages currently upgrading and using imagesloaded with masonry will encounter this problem. Furthermore, the current fix is not persistent, as the file will be overwritten with the original during the next upgrade.

r-anwar avatar Jan 07 '24 09:01 r-anwar