【问题标题】:moving card from one pile to another moving item from one array to another javascript将卡片从一堆移动到另一个移动项目从一个数组到另一个 javascript
【发布时间】:2011-07-06 12:09:23
【问题描述】:

首先,没有 DOM,这是在服务器端。 我的问题与以下内容几乎相同:Elegant technique to move items from one array to another 遗憾的是在 Javascript 中没有队列。

我有一堆卡片,表示为一个名为 Deck 的数组中的对象。

我想以一种干净的方式从甲板移动到手。我可以对手数组使用拼接和连接。

但我不想一直这样做。理想情况下,我会有一个可以传入卡片对象和目标数组的函数:

function moveCard (deck[5], hand) {
  // return success
}

或者有什么不同的结构可以更好的对 JS 中的数据进行建模?

谢谢。

【问题讨论】:

    标签: javascript arrays node.js


    【解决方案1】:

    如果您希望将其用作队列,您可以使用 Array.push 和 Array.shift 将数组用作队列。

    例如:

    var x = [];
    x.push("a"); // x = [ "a" ]
    x.shift(); // returns "a", x = []
    x.push("a"); // x = [ "a" ]
    x.push("b"); // x = [ "a", "b" ]
    x.shift(); // returns "a", x = [ "b" ]
    x.shift(); // returns "b", x = []
    

    你可以从甲板上移开并推到手上。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多