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 trialNathon Reed
4,810 Pointsin 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>';
$('#weather').html(forecastHTML);
is it possible to set i here not to be greater than 7 so I only display 7 days of data
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi 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
dxt
16,505 PointsI 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.
Nathon Reed
4,810 PointsI 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.
Nathon Reed
4,810 PointsNathon Reed
4,810 PointsThank you jason I was able to remove the regualr JS and used the jquery with the slice method thank you again for the help.