Object属性键与另一个属性值javaScript相同

时间:2018-02-11 09:02:45

标签: javascript screeps

我有这个对象

            Memory.creepsConf = {
            //The role by which we will refer to the creep
            roles: {
                harvester: "Harvester",//Harvests energy and gives it to the spawn
                upgrader: "Upgrader",//Harvests energy and gives it to the Controller
                builder: "Builder",// Harvests energy and builds stuff
                healer: "Healer"// Harvests energy and heals
            },
            //the maximum number of creeps. Used by ControllerCreeps
            maximum: {
                harvester: 100,
                upgrader: 100,
                builder: 100,
                healer: 100
            },
            //The bare minimum needed. Used by ControllerCreeps
            minimum: {
                harvester: 20,
                upgrader: 10,
                builder: 5,
                healer: 2,
            },
            //Since not all creeps roles will be filled the Colony needs to know
            //which creeps are a priority.
            priority: {
                harvester: 10,
                upgrader: 20,
                builder: 8,
                healer: 7
            },
            build: {
                harvester: [CARRY,WORK,MOVE],
                upgrader: [CARRY,WORK,MOVE],
                builder: [CARRY,WORK,MOVE],
                healer: [MOVE,HEAL,MOVE]
            }
        }

正如您在roles中所注意到的那样,我定义了角色,而在其他部分,我指的是每个角色的蠕变。

您可以注意到的另一件事是我总是使用roles中定义的密钥而不是值。这是一个问题,因为如果有人向我提供“收割机”,我需要从roles中取出密钥,然后使用密钥...使值过时。

我想要做的不是说harvester这是roles中的一个关键字我想将该键的值称为其他对象中的键

像这样的东西

            Memory.creepsConf = {
            //The role by which we will refer to the creep
            roles: {
                harvester: "Harvester",//Harvests energy and gives it to the spawn
                upgrader: "Upgrader",//Harvests energy and gives it to the Controller
                builder: "Builder",// Harvests energy and builds stuff
                healer: "Healer"// Harvests energy and heals
            },
            //the maximum number of creeps. Used by ControllerCreeps
            maximum: {
                Memory.creepsConf.roles.harvester: 100,
                Memory.creepsConf.roles.upgrader: 100,
                Memory.creepsConf.roles.builder: 100,
                Memory.creepsConf.roles.healer: 100
            },
            //The bare minimum needed. Used by ControllerCreeps
            minimum: {
                Memory.creepsConf.roles.harvester: 20,
                Memory.creepsConf.roles.upgrader: 10,
                Memory.creepsConf.roles.builder: 5,
                Memory.creepsConf.roles.healer: 2,
            },
            //Since not all creeps roles will be filled the Colony needs to know
            //which creeps are a priority.
            priority: {
                Memory.creepsConf.roles.harvester: 10,
                Memory.creepsConf.roles.upgrader: 20,
                Memory.creepsConf.roles.builder: 8,
                Memory.creepsConf.roles.healer: 7
            },
            build: {
                Memory.creepsConf.roles.harvester: [CARRY,WORK,MOVE],
                Memory.creepsConf.roles.upgrader: [CARRY,WORK,MOVE],
                Memory.creepsConf.roles.builder: [CARRY,WORK,MOVE],
                Memory.creepsConf.roles.healer: [MOVE,HEAL,MOVE]
            }
        }

我想要最终得到的是Memory.creepsConf.roles.*作为其他对象中表示的键的值,这样如果有人向我提供值Harvester,我实际上可以将它作为获取的关键所有需要的信息。

然而,第二段代码不起作用。我得到了

  

意外的令牌。

有没有办法可以将Memory.creepsConf.roles.*的值用作Memory.creepsConf.maximumMemory.creepsConf.minimumMemory.creepsConf.priorityMemory.creepsConf.build中的关键字?

如果这个例子太大而且难以遵循,我会尝试简化它

var obj = {
    foo:"Foooo",
    obj.foo: "Wohooo"
}

此对象现在应该有一个Fooooobj['Foooo']应返回“Wohooo”的键

3 个答案:

答案 0 :(得分:2)

为什么不将角色用作主配置对象的

Memory.config.creeps = {
    "Harvester": {
        maximum: 100, 
        minimum: 20, 
        priority: 10, 
        build: [CARRY, WORK, MOVE]
    }, {
    "Upgrader": {
        maximum: 100, 
        minimum: 10, 
        priority: 20, 
        build: [CARRY, WORK, MOVE]
    }
    ///...
};

现在你可以访问这样的属性:

function getMinimumForRole(role){
    if (role in Memory.config) {
        return Memory.config[role].minimum;
    }
    throw "role " + role + " not found";
}

如果你仍想继续你的工作方式,那么你可以使用一些ES6语法功能进行两项任务:

const roles = { // temporary variable for keeping the rest short
    harvester: "Harvester",
    upgrader: "Upgrader",
    builder: "Builder",
    healer: "Healer"
};
Memory.creepsConf = {
    roles, // ES6 short notation
    maximum: {
        [roles.harvester]: 100, // ES6 computed property syntax
        [roles.upgrader]: 100,
        [roles.builder]: 100,
        [roles.healer]: 100
    },
    // ...etc
};

答案 1 :(得分:0)

如果我是你,我会遵循Tomalak的建议,它不是更合乎逻辑,但它看起来更“面向对象”,因此它可能更容易使用:

Memory.config.creeps = [{
  role: "Harvester", 
  maximum: 100, 
  minimum: 20, 
  priority: 10, 
  build: [CARRY, WORK, MOVE]
}, {
  role: "upgrader",
  ...
}];

想想你将如何在Java中完成它,通常你需要创建相同类型的对象的集合,因为它发生了,我们有一组creeps配置。

如果你想轻松访问对象,你可以使用一个额外的地图,这对我来说非常好(请注意,我会保留数组,因为你可能需要根据某些标准搜索特定的配置):< / p>

Memory.config.maps.creeps = {
  "Harvester": Memory.config.creeps[0],
  "Upgrader": ...
};

答案 2 :(得分:-1)

谢谢大家的建议,但我通过一些实验找到了答案。虽然我必须说......我不知道它为什么会起作用......

         Memory.creepsConf = {
        //The role by which we will refer to the creep
        roles: {
            harvester: "Harvester",//Harvests energy and gives it to the spawn
            upgrader: "Upgrader",//Harvests energy and gives it to the Controller
            builder: "Builder",// Harvests energy and builds stuff
            healer: "Healer"// Harvests energy and heals
        },
        //the maximum number of creeps. Used by ControllerCreeps
        maximum: {
            [Memory.creepsConf.roles.harvester]: 100,
            [Memory.creepsConf.roles.upgrader]: 100,
            [Memory.creepsConf.roles.builder]: 100,
            [Memory.creepsConf.roles.healer]: 100
        },
        //The bare minimum needed. Used by ControllerCreeps
        minimum: {
            [Memory.creepsConf.roles.harvester]: 20,
            [Memory.creepsConf.roles.upgrader]: 10,
            [Memory.creepsConf.roles.builder]: 5,
            [Memory.creepsConf.roles.healer]: 2,
        },
        //Since not all creeps roles will be filled the Colony needs to know
        //which creeps are a priority.
        priority: {
            [Memory.creepsConf.roles.harvester]: 10,
            [Memory.creepsConf.roles.upgrader]: 20,
            [Memory.creepsConf.roles.builder]: 8,
            [Memory.creepsConf.roles.healer]: 7
        },
        build: {
            [Memory.creepsConf.roles.harvester]: [CARRY,WORK,MOVE],
            [Memory.creepsConf.roles.upgrader]: [CARRY,WORK,MOVE],
            [Memory.creepsConf.roles.builder]: [CARRY,WORK,MOVE],
            [Memory.creepsConf.roles.healer]: [MOVE,HEAL,MOVE]
        }
    }

这完全没问题。我发布的第二个错误的唯一区别是我将Memory.creepsConf.roles.*[]包围起来并且它有效。

显然......你可以在声明它的同时引用对象内的对象......你只需要在它周围添加[]

这一切有什么好处?

好处是,如果我需要通过提供的角色获取有关creeps的信息(比如说“Harvester”),我可以执行此操作Memory.creepsConf.maximum[role],它将为我提供所需的信息。

我不需要使用map或在获得价值时获取密钥,或者要求某人向我提供密钥而不是价值或做类似的事情。

简单的1行代码我可以获得所需的信息。这就是我想要的第一个。

现在......有些人提到在声明期间无法引用对象内部的对象,所以我有另一个问题 - 为什么这样做?