Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) AJAX and APIs Displaying the Photos

in the $.each loop is there a way to set i not to e greater than a number.

$.each(data.forecast.simpleforecast.forecastday, function(i, forecastday){
            forecastHTML += '<li class="grid-seventh-day">';

        });//each loop
        forecastHTML += '</ul>';

is it possible to set i here not to be greater than 7 so I only display 7 days of data

3 Answers

Hi Nathon,

You could call the slice method on the forecastday array to get it down to 7 elements. Then $.each would only iterate over those 7 elements.

Something like data.forecast.simpleforecast.forecastday.slice(0, 7) if you wanted the first 7 elements.

A simplified example to illustrate:

var days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$.each(days.slice(0, 7), function(i, forecastday){
  console.log(forecastday); // logs 1 2 3 4 5 6 7
});//each loop

Thank you jason I was able to remove the regualr JS and used the jquery with the slice method thank you again for the help.

I could be wrong. I believe the $.each() iterates internally, so you don't necessarily have direct control of it. You just get access to the current index for each iteration. From what I can figure, you just test if the index reaches 7 within the callback function and return false if it does.

I was able to get the with out using jquery just wrote a while loop in straight JS and got the same results, I just wanted to to use jquerry . Thank you for the help Daivd.