gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:45:37 25 4
gpt4 key购买 nike

我有这个对象

            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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com