【发布时间】:2016-06-27 16:30:24
【问题描述】:
我知道我可以将整数或字符放入队列或堆栈中,但是推送整个数组怎么样?
var stack = [];
stack.push(2); // stack is now [2]
stack.push(5); // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i); // displays 5
var queue = [];
queue.push(2); // queue is now [2]
queue.push(5); // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i); // displays 2
假设我有数据从客户端发送到服务器,服务器需要存储它们以便以后发布它们。我正在发送三个字段,username、message 和 avatar。
例子:
['simon','this is a message','avatar.png']
和
['Muray','this is another message','avatar2.png']
这两个数组应该被发送到服务器并在需要时弹出整个数组。
【问题讨论】:
-
问题在哪里?
-
数组可以包含任何你想要的东西,包括其他数组。
-
你是在问能不能把数组压入数组?如果是,那么是的,您可以将数组推入数组中。
-
你已经写过栈的概念了(:请阅读:bennadel.com/blog/…
-
为什么不在浏览器控制台中尝试一下,看看会发生什么?
标签: javascript arrays queue