javascript - How do I iterate over a queue, adding and deleting elements as I go? -
i want able iterate on queue, each time adding new elements queue, removing elements have dealt with.
queue = [[0,8],[1,2],[2,4]] [x,y] in queue in [1,2,3] # results in new coordinate.. queue.push([newx,newy])
the problem is, not sure best way be.
if delete each element array iterate, leaves empty element in array.
if copy array, empty doing queue.length = 0
, iterate on copy, wont work because doing slice copy doesn't work when array contains objects.
what correct way this?
what should modify copy of array:
queue2 = queue.slice 0 [x,y] in queue in [1,2,3] # generate newx , newy queue2.push([newx,newy]) queue = queue2
i'm not sure mean when say
that wont work because doing slice copy doesn't work when array contains objects.
you may have been misled read elsewhere. using slice
array copy works objects:
coffee> queue = [[0,8],[1,2],[2,4]] [ [ 0, 8 ], [ 1, 2 ], [ 2, 4 ] ] coffee> queue.slice 0 [ [ 0, 8 ], [ 1, 2 ], [ 2, 4 ] ]
what won't do deep copy of objects stored array. since you're doing insertions , deletions queue
, that's acceptable.
Comments
Post a Comment