Can I start looping from the 3rd item, or any other item, in an array with the jquery $.each()and also with $(elem).each(). I want both examples. #tableau is the id of a <table> in HTML.
See my code below:
var tableau = $('#tableau'),
tds = tableau.find('td'),
tdLen = tds.length,
superMarios = [
'Super Mario Bros',
'Super Mario Bros 2',
'Super Mario Bros 3',
'Super Mario World',
'Super Mario World 2: Yoshi\'s Island',
'Super Mario Galaxy',
'New Super Mario Bros Wii',
'Super Mario Galaxy 2'
],
superMarioCollection = superMarios.length;
$.each(superMarios, function(i, val){
console.log(i + ': ' + val);
tds.eq(i).append(val);
});
Many Thanks
You can’t start from anything but
0with$.each(), but you can prevent the code inside from running by using anif()statement.Or if you want to start on the third, but you want the indices to start with
0, then just take a.slice()of the Array, giving it the zero-based index where you want to begin.Or as an alternate to the first example, just offset the
ithe same amount as your starting point to give you the higher starting index: