gpt4 book ai didi

JavaScript:如何使用嵌套对象的变量名称创建嵌套对象?

转载 作者:行者123 更新时间:2023-11-28 06:21:43 25 4
gpt4 key购买 nike

如何使用变量创建嵌套对象。在本例中,我有一个在全局上下文中创建的名为“手”的对象。现在我有 x 个玩家(在本例中为 3 个),我为每个玩家发 5 张牌。

所以我希望手对象看起来像手[playerNum][cardInst][dealtCard]作为最终结果。我习惯了 Perl 哈希,这在 Perl 中非常简单。

但是,无论我如何尝试将元素放入手中,我都看不到卡片。请参阅下面的代码。 var 手 = {};在函数外部声明。我缺少什么?

function dealCards() {
console.log("Dealing Cards\r\n");
var numPlayers = 3;
var curCard;

for (i=0;i<5;i++) {
var cardInst = i.toString();

for (pn =0;pn<numPlayers;pn++){
// get current value for pn and store it
var playerNum = pn.toString();

//get card off of game deck
curCard = gameDeck.shift();

//add card info to variables
var dcardID = baseDeck[curCard].id;
var dcardName = baseDeck[curCard].name;
var dcardText = baseDeck[curCard].text;
var dcardImg = "none";

//create new card object
var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);



hands[playerNum][cardInst] = dealtCard;
console.log("blah");
}
}
console.log(hands);
}

function card(id,name,text,img){
this.id = id;
this.name = name;
this.text = text;
this.img = img;
}

var hands = {};



function dealCards() {
console.log("Dealing Cards\r\n");
var numPlayers = 3;
var curCard;

for (i=0;i<5;i++) {
var cardInst = i.toString();

for (pn =0;pn<numPlayers;pn++){
// get current value for pn and store it
var playerNum = pn.toString();

//get card off of game deck
curCard = gameDeck.shift();

//add card info to variables
var dcardID = baseDeck[curCard].id;
var dcardName = baseDeck[curCard].name;
var dcardText = baseDeck[curCard].text;
var dcardImg = "none";

//create new card object
var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);



hands[playerNum][cardInst] = dealtCard;
console.log("blah");
}
}
console.log(hands);
}

function card(id,name,text,img){
this.id = id;
this.name = name;
this.text = text;
this.img = img;
}

如有任何帮助,我们将不胜感激!

Z

最佳答案

有两件事; Hands[playerNum] 没有被初始化,并且函数 card() 没有返回值在添加之前尝试初始化内部对象(使用关键的playerNum);

function dealCards() {
console.log("Dealing Cards\r\n");
var numPlayers = 3;
var curCard;

for (i=0;i<5;i++) {
var cardInst = i.toString();

for (pn =0;pn<numPlayers;pn++){
// get current value for pn and store it
var playerNum = pn.toString();

//get card off of game deck
curCard = gameDeck.shift();

//add card info to variables
var dcardID = baseDeck[curCard].id;
var dcardName = baseDeck[curCard].name;
var dcardText = baseDeck[curCard].text;
var dcardImg = "none";

//create new card object
var dealtCard = new card(dcardID,dcardName,dcardText,dcardImg);

if (hands[playerNum] === undefined){
hands[playerNum] = {};
}



hands[playerNum][cardInst] = dealtCard;
console.log("blah");
}
}
console.log(hands);
}
function card(id,name,text,img){
this.id = id;
this.name = name;
this.text = text;
this.img = img;
return this;
}

另外,我个人不喜欢使用“this dot property”的构造函数风格。返回新对象是一个更好的习惯,这样您就不会意外地从当前“this”上下文中捕获其他属性。

function card(id,name,text,img){
return {
id: id,
name: name,
text: text,
img: img
}
}

关于JavaScript:如何使用嵌套对象的变量名称创建嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471643/

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