我有以下字典:
var deckDictionary = [
"card1": ["ace","hearts"],
"card2": ["ace","spades"],
"card3": ["ace","diamonds"],
"card4": ["ace","clubs"],
]
我想要做的是将cardSuit2
设置为等于数组中的第二个对象{key} "card2"
(伪代码):
var cardKey2 = "card2"
var cardSuit2 = whatever the card suit value is at cardKey2 ([[deckDictionary[1]][1]]?)
因此,例如在这种情况下,cardSuit2
将等于数组中具有键"card2"
的第二个对象,因此"spades"
。我该怎么做呢?
答案 0 :(得分:1)
如果您只想获得问题中指出的元素的值,则可以这样做:
var cardKey2 = "card2"
var cardSuit2 = deckDictionary[cardKey2]?.last