Javascript:对象数组的散列(使用Deck of Cards示例)

时间:2014-11-27 13:50:21

标签: javascript arrays sorting hash

我想了解如何正确创建一个物体数组哈希。

在这个例子中,我有一副牌(我省略了一些牌来缩短例子):

var deckOfCards = [
  {color: "red", coat: "hearts", id: "Ace", faceValue: "A"},
  {color: "black", coat: "clubs", id: "Ace", faceValue: "A"},
  {color: "red", coat: "diamonds", id: "Ace", faceValue: "A"},
  {color: "black", coat: "spades", id: "Ace", faceValue: "A"},
  {color: "red", coat: "hearts", id: "Two", faceValue: "2"},
  {color: "black", coat: "clubs", id: "Two", faceValue: "2"},
  {color: "red", coat: "diamonds", id: "Two", faceValue: "2"},
  {color: "black", coat: "spades", id: "Two", faceValue: "2"},
  {color: "red", coat: "hearts", id: "Three", faceValue: "3"},
  {color: "black", coat: "clubs", id: "Three", faceValue: "3"},
  {color: "red", coat: "diamonds", id: "Three", faceValue: "3"},
  {color: "black", coat: "spades", id: "Three", faceValue: "3"},
  {color: "red", coat: "hearts", id: "Jack", faceValue: "J"},
  {color: "black", coat: "clubs", id: "Jack", faceValue: "J"},
  {color: "red", coat: "diamonds", id: "Jack", faceValue: "J"},
  {color: "black", coat: "spades", id: "Jack", faceValue: "J"},
  {color: "red", coat: "hearts", id: "Queen", faceValue: "Q"},
  {color: "black", coat: "clubs", id: "Queen", faceValue: "Q"},
  {color: "red", coat: "diamonds", id: "Queen", faceValue: "Q"},
  {color: "black", coat: "spades", id: "Queen", faceValue: "Q"},
  {color: "red", coat: "hearts", id: "King", faceValue: "K"},
  {color: "black", coat: "clubs", id: "King", faceValue: "K"},
  {color: "red", coat: "diamonds", id: "King", faceValue: "K"},
  {color: "black", coat: "spades", id: "King", faceValue: "K"}
];

我现在定义了我想要用于分组卡片的不同哈希......

var hashOfCardsByColor = [];
var hashOfCardsByCoat = [];
var hashOfCardsById = [];
var hashOfCardsByFaceValue = [];

当我遍历卡片组时,我想:

  1. 在尝试将卡放入哈希值之前检查哈希键是否存在,如果密钥不存在,则创建它。
  2. 将卡放入可以通过该密钥访问的卡阵列中。
  3. 我的代码......

      deckOfCards.forEach(function(d, i){
        // Handle coats...
        if (d.coat == "hearts") {
          // 1. If "hearts" key doesn't exist, create it and push card onto Array that it points to
          // 2. If "hearts" key does exist, just push card onto Array that it points to
        }
        else if (d.coat == "clubs") {
          // 1. If "clubs" key doesn't exist, create it and push card onto Array that it points to
          // 2. If "clubs" key does exist, just push card onto Array that it points to
        }
        else if (d.coat == "diamonds") {
          // 1. If "diamonds" key doesn't exist, create it and push card onto Array that it points to
          // 2. If "diamonds" key does exist, just push card onto Array that it points to
        }
        else (d.coat == "spades") {
          // 1. If "spades" key doesn't exist, create it and push card onto Array that it points to
          // 2. If "spades" key does exist, just push card onto Array that it points to
        }      
      });
    

    我在上面每个细分受众群的主体中对第1步和第2步的语法和代码进行了挣扎,并感谢您提供的任何帮助。

    此外,还有一个"数组哈希"正确的上述描述或者是#34; Hashmap",例如?

2 个答案:

答案 0 :(得分:2)

好兄弟就这么简单。

  var hash = {};
    deckOfCards.forEach(function (e) {
        hash[e.coat] = hash[e.coat] || [];
        hash[e.coat].push(e);
    });

您的结果将会像

一样

object {hearts:Array [6],clubs:Array [6],diamond:Array [6],spades:Array [6]}

只需复制粘贴n在控制台中查看。如果这个答案解决了你的问题必须投票并检查确定。

答案 1 :(得分:0)

试试这个:

if(typeof hashOfCardsByColor[d.color] === 'undefined'){
    hashOfCardsByColor[d.color] = [];
}
hashOfCardsByColor[d.color].push(d);