Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.