多个 objects 具有相同的密钥

multiple objects with the same key

我有一个 object 数组:

[object, object, object, object, object]

我想使用 object 标题为每个设置一个键:

{test:object, tester:object, foo:object, bar:object, test:object}

所以我可以说 array.test 而不是 array[0].title。 但是密钥可以是通用的,当有多个 object 具有相同的密钥时,它会替换原来的密钥。

如果不向键添加索引值,我想做的事情是不可能的吗?在这种情况下,我最初拥有的最简洁的解决方案 array[0].title

is there any way to set the same key to multiple objects without replacing it other than appending an index value to to the key?

。对象键是唯一的,这意味着一个键只能保存一个值。如果您想保存多个值,则可以为其分配数组值。喜欢 { key : [value1, value2 ...]} 但这对您的问题没有用。另外你不确定键值是什么,那就是 object/map.

的整个 objective

is the way im doing it the best way?

。对于你提到的问题,使用数组 data-structure/construct 是好的。