Hi, I have an issue that I have had a hard time figuring out with googling. I’m a SQL and Python guy, so I’m not too comfortable with jQuery, but hopefully you can help med with this. I’m using an image API to create an image carousel.


My jQuery to fetch the data looks like this:
Now, what I want to do is get all previews[7].href from every data[0..n] (there are more than two). The only thing I can’t figure out is how to iterate through every instance of data to find all the previews[7].href. Any help is greatly appreciated! Thank you.
To clarify, it seems your specific question is how to iterate over javascript arrays?
If that’s the case there are several routes. jQuery has a forEach method. But so does javascript also. The Array prototype has forEach so you could do something like:
If you wanted to add each of those items to another array, you could declare that outside the loop and then just push each item into that array from within the loop.
There’s a more straight-foward way to do that with array.map which will let you map your original array into a new one with the data you want. Another options is array.reduce.
I recommend taking a look at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
PS. You’ll want to make sure your objects are arrays before doing any array methods on them to catch any errors.
Yes, it is about how to iterate over JavaScript arrays. I asked it here because I want a jQuery way of doing it.
Adding the items to another array seems like what I need. It’s the ‘drill down’ way from your snippet here I’ve been looking for but couldn’t figure out how to write it properly.
I’ll test this once I have the chance. Thank you! And thanks for the reading tips.
Presented like that yes… im not aware of any special function in jQuery that would allow you to avoid this.
However 2 comments:
do you have the possibility to modify the backend to send you only the information you need?


could you retrieve only the images you are interested in and not the whole set? e.g just a few images to preload?
Hi, unfortunately I cannot 1) modify the backend or 2) retrieve anything less then the whole set.
Thanks for the response!
Members
Online

source