html - javascript array restart counting -
i have piece of code:
//var data.name declared somewhere else, e.g. "sherlock". changes often. recents[recents.length] = data.name; idthis = "recent" + recents.length; if(recents.length >= 7) { recents[0]=recents[7]; recents[1]=recents[8]; recents[2]=recents[9]; recents[3]=recents[10]; recents[4]=recents[11]; recents[5]=recents[12]; recents[6]=recents[13]; recents[7]=recents[14]; recents[0]=recents[15]; recents[1]=recents[16]; recents[2]=recents[17]; //etc idthis = "recent" + (recents.length -7); } document.getelementbyid(idthis).innerhtml = data.name;
my question how automate recents[0]=recents[7] recents[1]=recents[8]
etc? point recent
id can't higher 6, otherwise rest of code won't work.
it sounds me want slice original array. i'm not sure slice want, here's how first 8 items , last 8 items, maybe 1 of want:
// first 8 items recents. var first8 = recents.slice(0, 8); // last 8 items recents. var last8 = recents.slice(-8); // first8 , last8 contain 8 items each.
of course, if recents
array doesn't have 8 items, results slice
less 8 items.
if want delete range in recents
array, can use splice
:
// delete first 8 items of recents. recents.splice(0, 8); // recents[0] value of former recents[8] (and on)
you can use return value of splice
items deleted:
// delete , first 8 items of recents. var deleteditems = recents.splice(0, 8); // add them end, example: recents = recents.concat(deleteditems);
Comments
Post a Comment