I want an Array method similar to Array.pop() that exhibits First In First Out behavior, instead of the native FILO behavior. Is there an easy way to do so?
Imagine a javascript console:
>> array = [];
>> array.push(1);
>> array.push(2);
>> array.push(3);
>> array.fifopop();
1 <-- array.pop() yields 3, instead
pop() The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
A Queue works on the FIFO(First in First Out) principle. Hence, it performs two basic operations that is addition of elements at the end of the queue and removal of elements from the front of the queue.
Shift() method removes the first element and whereas the pop() method removes the last element from an array. The Shift() returns the removed first element of the array. If the array is empty then this function returns undefined whereas the pop() method turns the removed element array.
Updated: Well arrays are neither LIFO or FIFO .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With