问题
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
回答1:
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
回答2:
The method is array.shift()
. It pulls the first array element much as array.pop()
pulls the last element.
来源:https://stackoverflow.com/questions/34622414/fifo-behavior-for-array-pop-in-javascript