gpt4 book ai didi

Javascript:对象数组的哈希(使用扑克牌示例)

转载 作者:行者123 更新时间:2023-12-02 16:45:05 25 4
gpt4 key购买 nike

我想了解如何正确创建对象数组的哈希值。

在此示例中,我有一副牌(我省略了一些牌以缩短示例):

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. 在尝试将卡放入哈希之前检查哈希 key 是否存在,如果 key 不存在,则创建它。
  2. 将卡片放入可以通过该键访问的卡片数组中。

我的代码...

  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 的语法和代码,感谢您提供的任何帮助。

此外,“数组哈希”是上述的正确描述还是“ HashMap ”?

最佳答案

好吧,兄弟,简单点。

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

您的结果将如下所示

对象{红心:数组[6],梅花:数组[6],方 block :数组[6],黑桃:数组[6]}

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

关于Javascript:对象数组的哈希(使用扑克牌示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27172247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com