javascript - add an element to the end of an array, deleting elements from the beginning to maintain the size? -
there variations on question none quite i'm looking for.
what simplest way in javascript add new elements end of array ( my_array.push(x) )?
and if array grows larger size (e.g. 100 elements) shift elements backwards (my_array[1] becomes my_array[0]) in order continue adding end of array.
push
returns length of array, can use compare against maximum desired length:
if (a.push(x) > max_length) a.shift();
Comments
Post a Comment