2017-04-21 57 views
1

我一直在寻找了一段时间,似乎无法找到一个答案,我面临的问题中的对象的值。我是新来的,所以我有一个相当高的机会,我只是在做我想做错的事情。我所做的目的只是为了了解我能做什么,不能做什么对象,数组和循环,所以当我用我想要做的事来完成这个项目时,我正在慢慢地想出如何去做这些东西。访问键和对象

所以我这里有一堆的对象(与键和值)。我将把这些单独的(Character)对象存储到另一个名为Characters的对象中。

function Character(sociability, assertiveness, adventurous, emotionalExpressiveness, kindness, altruism, affection, trust, impulseControl, thoughtfullness, emotionalStability, sexuality, evil, intellectualCuriosity, novelty, interests, intelligence) { 
    "use strict"; 
    this.sociability = sociability; 
    this.assertiveness = assertiveness; 
    this.adventurous = adventurous; 
    this.emotionalExpressiveness = emotionalExpressiveness; 
    this.kindness = kindness; 
    this.altruism = altruism; 
    this.affection = affection; 
    this.trust = trust; 
    this.impulseControl = impulseControl; 
    this.thoughtfullness = thoughtfullness; 
    this.emotionalStability = emotionalStability; 
    this.sexuality = sexuality; 
    this.evil = evil; 
    this.intellectualCuriosity = intellectualCuriosity; 
    this.novelty = novelty; 
    this.interests = interests; 
    this.intelligence = intelligence; 
} 

var aloy = new Character(0, 11, 11, 0, 11, 11, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0), 
    bayonetta = new Character(0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 17, 0, 11, 0, 0), 
    elizabeth = new Character(0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12), 
    ellie = new Character(0, 12, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 11, 10), 

//store all above objects into new object 
var Users = { 
    aloy: aloy, 
    bayonetta: bayonetta, 
    elizabeth: elizabeth, 
    ellie: ellie, 
}; 

在此之后我创建包括从Character对象键的5名数组:

var matchArr = ["sociability", "assertiveness", "adventurous", "emotionalExpressiveness", "kindness"] 

下面是我卡在一部分,我会试着解释尽我所能。我会先告诉你代码,然后尝试理解我想要做的事情。

for (i = 0; i <= 4; i++) { 
    for (var obj in Users) { 
     if (Users.obj.matchArr[0] > 1) { 
      console.log(obj) 
     } 
    } 
} 

我想要做的是遍历Users对象,如果对象(Characters)内的对象之一具有与满足我的if语句价值的关键,登录该字符对象的名称。

- 如果我登录for (var obj in Users) { console.log(obj); }我按照预期循环存储在Users中的每个Character名称。

- 如果我通过itslef登录matchArr[0]我得到"sociability"从我matchArr阵列的预期。

- 如果我登录Users.aloy.sociability我得到0如预期它的aloy.sociability

价值 - 但是,如果我登录Characters.aloy.matchArr[0]我得到一个错误(无法未定义读取属性“0”)。我不明白为什么这是不是明确打字sociability因为这是matchArr[0]精确值不同。

我如何正确引用Users.aloy.sociability例如以我试图的方式。在我的脑海中,当我参考Users.obj.matchArr[0]时,这与明确键入Users.aloy.sociability,Users.bayonetta.sociability`(等等,因为它循环)相同。

再说一次,我可能会对这个完全错误的事情进行讨论,即使它只是指向其他文档,我可以通读并获得更好的理解,这可能会使我朝着正确的方向发展,但我会感激任何帮助。

我唯一的猜测是数组值(例如matchArr[0] > "sociability")是一个字符串,我不能以这种方式使用字符串。如果是这样,我不知道该怎么办。

非常感谢您的帮助。

+0

请缩短您的代码示例。你只需要几个有几个属性的字符来说明你的问题。 –

回答

1

这个循环应该打印字符名称和字符匹配什么属性。

for (var key in Users) { 
    matchArr.forEach(function(el) { 
    if (Users[key][el] > 1) { 
     console.log(key + ' matches attribute of ' + el); 
    } 
    }); 
} 

工作code example on jsfiddle

+0

这让我指出了正确的方向,我能够适应我需要的东西。我能够省略我刚刚使用的FOR循环,只是使用“for(var key in Users){}”部分。最大的问题看起来像我在写其他错误。把它写成用户[key] [matchArr [0]]正在为我现在需要的东西而工作。非常感谢你。 –

0

让我们来看看:

//loop each Users 

for(character in Users) { 
    //Loop each matchArr 
    matchArr.forEach(function(skill) { 
    //at this point we are testing something like this: 
    //If Users['aloy']['sociability'] > 1  
    if(Users[character][skill] > 1) { 
     //log aloy 
     console.log(character); 
    } 
    } 
}