Voglio un metodo Array simile a Array.pop () che mostri il comportamento First In First Out, invece del comportamento FILO nativo. C'è un modo semplice per farlo?
Immagina una console javascript:
>> array = [];
>> array.push(1);
>> array.push(2);
>> array.push(3);
>> array.fifopop();
1 <-- array.pop() yields 3, instead
risposte:
12 per risposta № 1Puoi usare 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
1 per risposta № 2
Il metodo è array.shift()
. Estrae il primo elemento dell'array tanto quanto array.pop()
tira l'ultimo elemento.