FIFO behavior for Array.pop in javascript? [duplicate]

元气小坏坏 提交于 2019-12-10 12:38:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!