对象 属性 键与另一个 属性 值 javaScript 相同
Object property key same as another property value javaScript
我有这个对象
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
中注意到的那样,我定义了角色,在其他部分我按角色引用了每个 creep。
您可以注意到的另一件事是我总是使用 roles
中定义的键而不是值。这是一个问题,因为如果有人向我提供 "Harvester",我需要从 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
我实际上可以将它用作键获取所有需要的信息。
然而,第二段代码不起作用。我得到
Unexpected token .
有没有办法使用 Memory.creepsConf.roles.*
的值作为 Memory.creepsConf.maximum
、Memory.creepsConf.minimum
、Memory.creepsConf.priority
和 Memory.creepsConf.build
中的键?
如果这个例子太大而难以理解,我会尽量简化它
var obj = {
foo:"Foooo",
obj.foo: "Wohooo"
}
这个对象现在应该有一个键 Foooo
并且 obj['Foooo']
应该 return "Wohooo"
如果我是你,我会听从 Tomalak 的建议,它不一定更合乎逻辑,但看起来更 "object oriented",因此可能更容易使用:
Memory.config.creeps = [{
role: "Harvester",
maximum: 100,
minimum: 20,
priority: 10,
build: [CARRY, WORK, MOVE]
}, {
role: "upgrader",
...
}];
想一想在 Java 中你会怎么做,你经常需要收集相同类型的对象,碰巧,我们有一个 creep 配置的集合。
如果您想简化对对象的访问,您可以使用额外的地图,这对我来说非常好(请注意,我会保留数组,因为您可能需要根据某些条件搜索特定的配置) :
Memory.config.maps.creeps = {
"Harvester": Memory.config.creeps[0],
"Upgrader": ...
};
谢谢大家的建议,但我通过一些试验找到了答案。虽然我必须说...我不知道它为什么有效...
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.*
并且它有效。
显然...您可以在声明对象时在对象内部引用对象...您只需要在其周围添加 []
这一切有什么好处?
好处是,如果我需要按提供的角色获取有关 creep 的信息(比方说 "Harvester"),我可以这样做 Memory.creepsConf.maximum[role]
,它会给我所需的信息。
我不需要使用 map
或在我有值时获取密钥或要求他人提供密钥而不是值或做任何类似的事情。
只需一行代码,我就可以获得所需的信息。这就是我最初想要的。
现在...你们中的一些人提到在声明期间无法引用对象内部的对象,所以我有另一个问题 - 为什么这样做有效?
为什么不使用角色作为主配置对象的键:
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
};
我有这个对象
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
中注意到的那样,我定义了角色,在其他部分我按角色引用了每个 creep。
您可以注意到的另一件事是我总是使用 roles
中定义的键而不是值。这是一个问题,因为如果有人向我提供 "Harvester",我需要从 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
我实际上可以将它用作键获取所有需要的信息。
然而,第二段代码不起作用。我得到
Unexpected token .
有没有办法使用 Memory.creepsConf.roles.*
的值作为 Memory.creepsConf.maximum
、Memory.creepsConf.minimum
、Memory.creepsConf.priority
和 Memory.creepsConf.build
中的键?
如果这个例子太大而难以理解,我会尽量简化它
var obj = {
foo:"Foooo",
obj.foo: "Wohooo"
}
这个对象现在应该有一个键 Foooo
并且 obj['Foooo']
应该 return "Wohooo"
如果我是你,我会听从 Tomalak 的建议,它不一定更合乎逻辑,但看起来更 "object oriented",因此可能更容易使用:
Memory.config.creeps = [{
role: "Harvester",
maximum: 100,
minimum: 20,
priority: 10,
build: [CARRY, WORK, MOVE]
}, {
role: "upgrader",
...
}];
想一想在 Java 中你会怎么做,你经常需要收集相同类型的对象,碰巧,我们有一个 creep 配置的集合。
如果您想简化对对象的访问,您可以使用额外的地图,这对我来说非常好(请注意,我会保留数组,因为您可能需要根据某些条件搜索特定的配置) :
Memory.config.maps.creeps = {
"Harvester": Memory.config.creeps[0],
"Upgrader": ...
};
谢谢大家的建议,但我通过一些试验找到了答案。虽然我必须说...我不知道它为什么有效...
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.*
并且它有效。
显然...您可以在声明对象时在对象内部引用对象...您只需要在其周围添加 []
这一切有什么好处?
好处是,如果我需要按提供的角色获取有关 creep 的信息(比方说 "Harvester"),我可以这样做 Memory.creepsConf.maximum[role]
,它会给我所需的信息。
我不需要使用 map
或在我有值时获取密钥或要求他人提供密钥而不是值或做任何类似的事情。
只需一行代码,我就可以获得所需的信息。这就是我最初想要的。
现在...你们中的一些人提到在声明期间无法引用对象内部的对象,所以我有另一个问题 - 为什么这样做有效?
为什么不使用角色作为主配置对象的键:
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
};