Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问对象中对象的键和值_Javascript_Arrays_Object - Fatal编程技术网

Javascript 访问对象中对象的键和值

Javascript 访问对象中对象的键和值,javascript,arrays,object,Javascript,Arrays,Object,我四处寻找了一段时间,似乎找不到我所面临问题的答案。我是新来的,所以我很有可能只是做了我想做的错事。我所做的只是为了了解我可以和不能用对象、数组和循环做什么,所以当我用我想做的来完成这个项目时,我正在慢慢地弄清楚如何做这些事情 我这里有一堆对象(带键和值)。我将把每个单独的(角色)对象存储到另一个称为角色的对象中 function Character(sociability, assertiveness, adventurous, emotionalExpressiveness, kindnes

我四处寻找了一段时间,似乎找不到我所面临问题的答案。我是新来的,所以我很有可能只是做了我想做的错事。我所做的只是为了了解我可以和不能用对象、数组和循环做什么,所以当我用我想做的来完成这个项目时,我正在慢慢地弄清楚如何做这些事情

我这里有一堆对象(带键和值)。我将把每个单独的(角色)对象存储到另一个称为角色的对象中

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,
};
在此之后,我创建了一个数组,其中包含来自角色对象的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)
        }
    }
}
(i=0;i 1)的
{
控制台日志(obj)
}
}
}
我要做的是循环遍历
Users
对象,如果该对象中的一个对象(
字符
)具有一个值满足我的
if
语句的键,则记录该字符对象的名称

-如果我为(Users中的var obj)记录
{console.log(obj);}
我会像预期的那样遍历存储在
用户
中的每个
字符
名称

-如果我通过itslef记录
matchArr[0]
,我将从
matchArr
数组中获得
预期的“社交性”

-如果我记录
Users.aloy.sociability
我会得到
0
,因为这是
aloy.sociability

-但是如果我记录Characters.aloy.matchArr[0]我会收到一个错误(无法读取未定义的属性“0”)。我不明白为什么这与显式键入
sociability
不同,因为这是
matchArr[0]
的确切值

我如何正确引用
用户.aloy.sociability
,例如,我正在尝试的方式。在我看来,当我引用
Users.obj.matchArr[0]
时,它与显式键入
Users.aloy.sociability
、Users.bayonteta.sociability`(循环时如此)是有道理的

再说一遍,我可能是完全错误的,我会感谢任何帮助,即使它只是指向我可以通读的其他文档,并获得更好的理解,这可能会引导我走向正确的方向

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

提前感谢您的帮助。

让我们看看:

//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);
    }
  }
}

这个循环应该打印字符名和字符匹配的属性

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

工作。

请缩短代码示例。你只需要几个字符和几个属性来说明你的问题。这让我找到了正确的方向,我能够根据需要调整它。我能够省去我使用的最初FOR循环,只使用“FOR(var-key-in-Users){}”部分。最重要的是,我似乎把剩下的写错了。以用户身份编写[key][matchArr[0]]符合我现在的需要。非常感谢你。