FIFO behavior for Array.pop in javascript?

I need an Array method similar to Array.pop (), which demonstrates First In First Out behavior, not the native FILO behavior. Is there an easy way to do this?

Imagine a javascript console:

>> array = []; >> array.push(1); >> array.push(2); >> array.push(3); >> array.fifopop(); 1 <-- array.pop() yields 3, instead 
+9
source share
2 answers

You can use array.prototype.shift ()

 >> array = []; >> array.push(1); >> array.push(2); >> array.push(3); >> array.shift(); //outputs 1 and removes it from the array 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

+23
source

array.shift() method. It pulls the first element of an array in the same way that array.pop() pulls the last element.

+4
source

Source: https://habr.com/ru/post/1239937/


All Articles