JavaScript:如何使用嵌套对象的变量名称创建嵌套对象
JavaScript: How to create nested object using variable names for the nested objects?
如何使用变量创建嵌套对象。在本例中,我有一个名为 hands 的对象,它是在全局上下文中创建的。现在我有 x 个玩家(在本例中为 3 个),我每人发一手 5 张牌。
所以我希望手牌对象看起来像手牌[playerNum][cardInst][dealtCard]作为最终结果。我已经习惯了Perl Hashes,这在Perl中非常简单。
然而,无论我如何尝试将物品交到手中,我都没有看到卡片。 请参阅下面的代码。变量手 = {};在函数外部声明。 我错过了什么?
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() 没有返回在添加到内部对象之前,尝试初始化内部对象(使用 key 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获取嵌套对象中所有子对象的单个属性
- 如何打印嵌套对象的所有值
- 设置嵌套对象属性的更好方法
- 访问嵌套JSON对象的键,其中键是动态的
- 嵌套对象结构
- 如何递归地获取嵌套对象中所有子对象的列表
- 更改嵌套对象的父子相关id
- Node.js:用作对象嵌套元素名称的变量
- Sailsjs创建对象-嵌套创建
- Jquery $.ajax 获取响应与将“this”对象嵌套传递到“success”回调函数冲突
- 从嵌套属性数组中获取对象嵌套值
- 将对象嵌套在另一个数组中
- javascript对象嵌套回调
- 当被引用对象嵌套在ExtJS 5.0.0中时,如何读取它
- 如何在不知道其位置的情况下删除Mongo对象(嵌套的2个数组)
- 排序嵌套在对象中的数组,该对象嵌套在另一个对象中
- Javascript对象嵌套
- 在Angular中创建多维/嵌套对象(嵌套forEach循环)
- 仅列出jquery中包含json对象嵌套的所有键
- 如何将一个javascript对象嵌套在另一个对象中