我有一系列阵列"动物"我想用es6扩展语法编写两个函数入队并出列。 [FIFO]
我想要这个,因为这个对象处于我的redux状态,我不希望在入队和出队时发生突变。相反,我每次都会想要一个新的动物物品入队和出队。
let animals = [[cat][dog][elephant][mouse]]
enqueue(element)
{
animals = [...animals, element]
}
dequeue()
{
animals = [???]
}
答案 0 :(得分:2)
它不是运营商;它是一种语法。您可以通过执行以下操作来使用它来实现dequeue
:
let animals = ["cat", "dog", "elephant", "mouse"];
function enqueue(element) {
animals = [...animals, element];
}
function dequeue() {
let element;
[element, ...animals] = animals;
return element;
}
console.log(animals);
enqueue("frog");
console.log(animals);
console.log(dequeue());
console.log(animals);

.as-console-wrapper { max-height: 100% !important; top: 0; }