javascript - 对象属性键与另一个属性值 javaScript 相同

标签 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 中定义了 Angular 色,而在其他部分中,我按 Angular 色引用了每个 creep。

您会注意到的另一件事是,我总是使用 roles 中定义的键而不是值。这是个问题,因为如果有人向我提供“Harvester”,我需要从 roles 中获取 key ,然后使用 key ...使值过时。

我想做的不是说 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 我实际上可以将它用作获取所有需要的信息的 key 。

然而,第二段代码不起作用。我明白了

Unexpected token .

有没有办法使用 Memory.creepsConf.roles.* 的值作为 Memory.creepsConf.maximum 中的键,Memory.creepsConf。最小值Memory.creepsConf.priorityMemory.creepsConf.build ?

如果这个例子太大而难以理解,我会尽量简化它

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

这个对象现在应该有一个键,它是 Foooo 并且 obj['Foooo'] 应该返回“Wohooo”

最佳答案

为什么不使用 Angular 色作为主要配置对象的:

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

关于javascript - 对象属性键与另一个属性值 javaScript 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48729916/

相关文章:

javascript - Screeps - 无法访问蠕变内存

javascript - 寻找能量最高的结构

javascript - 对所有可能的子属性的访问器进行原型(prototype)设计

javascript - 由 d3 (d3.svg.arc) 创建的 SVG 路径元素在应用蒙版时消失

javascript - 有没有办法删除小兵?

javascript - Chrome 系统调用等待异步结果

javascript - 平铺 map 碰撞在 Javascript 中无法按预期工作

typescript - ts-jest - 运行 jest 时,从类型 (.d.ts) 中声明 const 值未定义

php - jQuery 解析 Twitter jSON 但不解析我的同一个 PHP 文档

javascript - 当call和apply嵌套在函数中时, "this"关键字指向什么?